Possible Duplicate:
Undefined Behavior and Sequence Points
In C++ on a machine code level, when does the postincrement++ operator get executed?
The precedence table indicates that postfix++ operators are level 2: which means in
int x = 0 ;
int y = x++ + x++ ; // ans: y=0
The postfix ++'s execute first.
However, it would seem that the logical operation of this line is the addition happens first (0+0), but how does that happen?
What I imagine, is the following:
// Option 1:
// Perform x++ 2 times.
// Each time you do x++, you change the value of x..
// but you "return" the old value of x there?
int y = 0 + x++ ; // x becomes 1, 0 is "returned" from x++
// do it for the second one..
int y = 0 + 0 ; // x becomes 2, 0 is "returned" from x++... but how?
// if this is really what happens, the x was already 1 right now.
So, the other option is although x++ is higher on the precedence table that x + x, the code generated due to x++ is inserted below the addition operation
// Option 2: turn this into
int y = x + x ; //
x++ ;
x++ ;
That second option seems to make more sense, but I'm interested in the order of operations here. Specifically, when does x change?