+ 4

How Increment work in this cout statement?

can someone explain why this code in c++ results 2210 int main (){ int x=0,y=0; cout << ++x << ++x; cout << y++ << y++; return 0; }

28th Sep 2017, 5:50 AM
Trendy Prima Wijaya
Trendy Prima Wijaya - avatar
8 Answers
+ 15
The order in which your x++ and ++x (or ++y and y++)statements are evaluated is "undefined", so is the effect of your code. Even if it seems to happen from right to left in your particular examples, you should not rely on that by any means. cout << ++x << ++x; //22 cout << y++ << y++; //10 cout << ++x; //1 cout << ++x; //2 cout << y++; //0 cout << y++; //1 [https://stackoverflow.com/questions/33445796/increment-and-decrement-with-cout-in-c]
28th Sep 2017, 6:03 AM
Babak
Babak - avatar
+ 10
Thank you so very much ChaoticDawg for clarification. 3 months ago a friend came up with the same question as this one and some folks including me guided him into wrong direction. So I just wanted to be a bit precise not disrespecting to your knowledge and skills. @~)~~~~
28th Sep 2017, 6:34 AM
Babak
Babak - avatar
+ 10
Thanks dear Hatsy. @~)~~~~
28th Sep 2017, 6:36 AM
Babak
Babak - avatar
+ 9
No ChaoticDawg that's not a correct evaluation in this case.
28th Sep 2017, 6:07 AM
Babak
Babak - avatar
+ 9
Yes Trendy. They are exactly evaluated in two different ways.
28th Sep 2017, 6:17 AM
Babak
Babak - avatar
+ 8
Thanks for the explanation @Babak Sheykhan. Just dropping another link: https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points
28th Sep 2017, 6:29 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
cout << ++x << ++x; //prefix First the rightmost ++x is performed and x is incremented to 1 then the leftmost ++x is performed and x is incremented to 2. Then the leftmost x value of 2 is inserted into the stream followed by the rightmost x value of 2. This results in 22 cout << y++ << y++; //postfix First the rightmost y++ is performed, the current value of y (0) is held in an implicit temp variable and the the value of y is then incremented to 1. Then the leftmost y++ is performed and the new value of y (1) is held in an implicit variable and then y is incremented to 2 (which is never used). Then the leftmost held value (1) is inserted into the stream followed by the rightmost held value (0). This results in 10 thus outputting 2210
28th Sep 2017, 6:03 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
@Babak I was intentionally vague and didn't go into detail about it so that it would be easier to understand, but yes it does depend on the compiler and its operator precedence, lvalue rvalue etc. The order is in this case however (mostly) correct. This is why the output is 2210 and not 2201, which is what it would be otherwise. This would also explain why even in the link you provided that the rightmost value is lower than the center value as they have the same precedence. Otherwise its output would be 7 4 5. I'm not saying that it's 100% correct (my c++ is a bit rusty), but it is a sound explaination from what I can remember from several years back .
28th Sep 2017, 6:28 AM
ChaoticDawg
ChaoticDawg - avatar