Here is the code compiled in dev c++ windows:
#include <stdio.h>
int main() {
int x = 5;
printf("%d and ", sizeof(x++)); // note 1
printf("%d\n", x); // note 2
return 0;
}
I expect x
to be 6 after executing note 1. However, the output is:
4 and 5
Can anyone explain why x
does not increment after note 1?
sizeof
is a compile-time builtin operator and is not a function. This becomes very clear in the cases you can use it without the parenthesis:sizeof()
operator gives size of the data-type only, it does not evaluate inner elements.sizeof
is a compile-time operator, so at the time of compilationsizeof
and its operand get replaced by the result value. The operand is not evaluated (except when it is a variable length array) at all; only the type of the result matters.Output:
as
short
occupies 2 bytes on my machine.Changing the return type of the function to
double
:will give
8
as output.