confusion with pre and post increment)

I commented next to the code how I worked out the output for this code. However actual output is different from what I've got. Somebody please explain me where I went wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
int f, x=10, y=10, i=2;
f = (x==y);
cout << "f is " << f << endl;    // f is 1
while (++i<5) f *= i;            
cout << "f is " << f << endl;    //while(4<5) f = 3*4=12
x = (x++ > y) ? (x+y) : (x-y);   //x=(11-10)
cout << "x is " << x << endl;    // x is 1
x = y++ + x++;                   //x = 10+ 1= 11 (since y and x are post-increment)
y = ++y + ++x;                   //y = 12+ 12=24 (since y and x are pre-increment)
cout << "x is " << x++ << endl;  // x = 12 (x is a post-increment)
cout << "y is " << ++y << endl;  //y=25  (y is a pre-increment)

}

This statement

x = y++ + x++;

has undefined behavior.
Topic archived. No new replies allowed.