Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
Undefined Behavior and Sequence Points
Ok we all know that i++ increments value by 1 on next line and ++i increments on same line
(please correct me if i am wrong there )
so for a sample statement of c as follows:
int a=0;
printf("%d , %d",++a,a);
the expected output should be 1 , 1
but instead it gives 1 , 0
so as one might guess what i am asking here is why does the second linking of
i
print 0
instead of 1
when the value is already incremented.
So if post increment didn't increment value in the same line then what is the
difference between post and pre increment?
edit : changed the name of variable from i to a to avoid grammatical confusion.
It's undefined behavior. the compiler is allowed to calculate the parameters in any order. your compiler just calculate it from right to left, so the rightest parameter is 0, and the 2nd is 1.
edit: as Seth said, the compiler is only free to change the order of calculating, not to do whatever it wants, so when you don't care about the order you can freely call functions, but you should never assume that one parameter is been calculated before another.