c questions confused

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
C:
#include<stdio.h>

int main() {

   int x = 1, y = 2;

  printf("Address of variable x : %d \n", &x);
  printf("Address of variable y : %d \n", &y);

  printf("size of variable x : %d \n", sizeof(x));
  printf("size of variable x : %d \n", sizeof(y));

  printf("x : %d \n", x);
  printf("y : %d \n", y);

  return 0;
}
Address of variable x : 6422316
Address of variable y : 6422312
size of variable x : 4
size of variable x : 4
x : 1
y : 2

x is integer variable and it occupy 4 bytes of memory. each byte is 8 bits.

x -> 8 bits -8 bits- 8 bits -8 bits

does x variable occupy memory space from 6422316 to 6422319 ?
 

bogosort

Joined Sep 24, 2011
696
does x variable occupy memory space from 6422316 to 6422319 ?
Yes, inclusive. Your code itself shows that the size of an int is 4 bytes.

By the way, it's convention to print memory addresses as unsigned integers in hexadecimal representation. The printf() function even has a format specifically for it: %p.
C:
#include<stdio.h>

int x[3] = {1, 2, 3};

int main() {

    int i;

    for ( i = 0; i < 3; ++i )
        printf("Address of x[%d]: %p\n", i, &x[i]);

    return 0;
}
On my system, this prints
Code:
Address of x[0]: 0x6008e0
Address of x[1]: 0x6008e4
Address of x[2]: 0x6008e8
You can see that each int requires 4 bytes of storage.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
What is the output of the following problem ?

C:
#include<stdio.h>
int main()
{
  int i, j;

  j = 10;
  i = j++ - j++;

  printf("%d %d", i,j);

  return 0;
}
Book answer is 0, 12

When I run program in my system It show following output
-1 12
line j = 10; of the program says j=10 . j++ line says j means post increment
so j = 10;
and i = j++ -j++ = 11 -12 = -1

I believe the answer given in the book is wrong and correct answer is -1 12 Am I correct ?
 

bogosort

Joined Sep 24, 2011
696
I believe the answer given in the book is wrong and correct answer is -1 12 Am I correct ?
As I see it, there are two reasonable arguments: (1) the book is correct, or (2) the expression "i = j++ - j++;" is undefined behavior.

In favor of (1), we might say that by definition of the post-increment operator, at the time of evaluation the value of "j++" is the value that 'j' had before the increment. That there are two such increments in the expression doesn't matter, as both "j++" statements evaluate to the same value (the increment doesn't happen until after the entire expression is complete, aka the sequence point). Thus, the final value of 'i' is unambiguously 0. Likewise, the final value of 'j' is unambiguously 10 + 1 + 1 = 12.

In favor of (2), we could argue that "i = j++ - j++;" is undefined simply because it invokes two unsequenced side-effects on a single variable. That your compiler and my compiler give different answers (mine says 0 and 12) is pretty good evidence that we've hit undefined behavior.

In any case, it should be very clear that you shouldn't use such constructs in your code. Hopefully the book used this as an example of what not to do.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
In any case, it should be very clear that you shouldn't use such constructs in your code. Hopefully the book used this as an example of what not to do.
I don't understand following program
C:
#include <stdio.h>     
int main()
{     
   if(printf("hello world")) // we check only condition 
   {
   }
  return 0;
}
hello world
program print hello world but I don't understand why It doesn't print
 

bogosort

Joined Sep 24, 2011
696
program print hello world but I don't understand why It doesn't print
The 'if' block executes when the condition of the 'if' statement is true, and in C, any integer value other than 0 is considered true. Here, the condition depends on the result of a function call, and in this case, the function is printf(). So, while evaluating the condition, printf() is called, which sends the string "hello world" to standard output and returns the number of characters printed: 10. Since 10 is not equal to 0, the 'if' block is entered, but since there are no statements in the block, nothing happens and code execution falls outside the block, to the "return 0;" statement.

As a side note, I take it that English is not your first language, and I sympathize with how difficult it can be to ask good questions in a non-native language. But sometimes it is difficult to understand what exactly you're asking. For example, in this case, you said "program print hello world", which indicates that the program does print. But you included the clause "I don't understand why It doesn't print", which indicates that the program does not print. As an aid to communication, I suggest that you try to be very specific (use more words) when asking a question and, in particular, what it is that is possibly confusing you. Just a suggestion. :)
 
Top