I have written the following prog.
int main()
{
int val = 4;
printf("size of val = %d, val = %d",sizeof(++val),val);
}
output: size of val = 2, val = 4
The output expected was : size of val = 2, val = 5
Can somebody explain why?
The order of evaluation of parameters to a function is
undefined in the standard. It is legal (and not uncommon)
that the last parameter, val, would be evaluated before
the next-to-last parameter, ++val .
The operation of ++val and val++ is defined in terms of
"sequence points": the increment takes place -sometime- between
the beginning of the sequence point and the end, with it being
undefined as to exactly when. If you attempt to use the same
variable within the same sequence point, it isn't defined as to
whether you will get the old or new value for the other
references -- it isn't even certain that you will always get
the same order for the same compiler (as compilers are allowed
to apply optimizations within sequence points.)
As a rough approximation, sequence points are usually present
at the beginning and end of expressions, but within expressions
only && and || force sequence points (and possibly the ','
sequencing operator... I use that so seldom that I
would have to look up it's properties to be sure.)