#include<stdio.h>
main()
{
int x=10,y;
y=sizeof(++x);
printf("x=%d\ny=%d\n",x,y);
}
Oput Put
x=10
y=4
why not x=11
"sizeof" operator does not evaluate the expression contained therein
it seems. I didnt know that : but purely because I never stuck an
expression in it for reasons unknown - I just assumed it as a
"preprocessor" type thing and just never did it. I can claim no genius.
It would be the same had you put
sizeof(x=3);
I would agree that it is a little confusing since the
compiler (gcc) happily lets you write something like
sizeof(x=funcCall(y)) too!
Looking at this in n1124pdf:
6.5.3.4 The sizeof operator
Constraints
2 The sizeof operator yields the size (in bytes) of its operand, which
may be an expression or the parenthesized name of a type. The size is
determined from the type of the operand. The result is an integer. If
the type of the operand is a variable length array type, the operand
is evaluated; otherwise, the operand is not evaluated and the result
is ....
There is a rather unhelpful footnote which states
"the operand of a sizeof operator is usually not evaluated"
Hopefully someone will enlighten us both as to what the standard means
: I just assumed it was a pre-processor type thing and would never
have thought the compiler would allow such a statement there.