0

Why when x=3 is x=++x + x++ 9?

int x= 3; x = ++x + x++; Isn't this undefined behavior? I've tried, x = 1 + x++; //4 x = x++; //3 x = ++x; //4 I think it should be 8, why wouldn't it be?

21st Jan 2017, 11:13 PM
Leon
Leon - avatar
8 Answers
+ 1
I looked around and some people claim that statements like ++x + x++; are undefined in the C and/or C++ standard, because the value of x is being changed twice within one statement (which they say is undefined in the standard), so what happens can be different depending on the compiler you're using. Kept looking, and in the copy of the standard I found here (warning 1366 page, 5 MB pdf): http://open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf In section 5.2.6 - Increment and decrement - [expr.post.incr]: "The value computation of the ++ expression is sequenced before the modification of the operand object. With respect to an indeterminately-sequenced function call, the operation of postfix ++ is a single evaluation." So I take that to mean that something like ++x + x++; is an "indeterminately-sequenced call," so the postfix ++ will end up incrementing the x on the right before the addition/assignment, which would explain 9. Both x's get incremented by the prefix ++ like you'd expect, but then the x on the right gets incremented once as well because of how it decides to handle sequencing when you're changing the value of the variable twice in one statement. As another illustration: int y = 3, z = 3; z = ++y + z++; cout << z; // Output: 7 y got an increment like you'd expect, and z didn't, like you'd expect. So it really has something to do with sequencing weirdness that comes up from doing both a postfix and prefix ++ to the same variable in one statement. At least that's my guess right now, I'm curious if anyone else can poke around that pdf or get a better explanation from elsewhere.
22nd Jan 2017, 1:01 AM
Potto
Potto - avatar
+ 2
Thanks for the great answer Potto. THIS PARAGRAPH IS MISGUIDED: That a side effect (post increment, on the lhs) from the rhs acts not only after its expression is evaluated but after it has been stored to the lhs seems very odd. Especially considering it doesn't in the case x = 1 + x++. cout << x + x++; is 7 in the playground, which further supports Potto's explanation, about chaining the same operand. Totally out of keeping with the whole notion of POST increment, which is a much neater maxim.
22nd Jan 2017, 1:49 AM
Leon
Leon - avatar
+ 1
i think that the pluses from x++ are added to the final result as well, because it is added to the same x after that equation, so we end up with 8 + 1
22nd Jan 2017, 12:09 AM
Petar Dimitrov
Petar Dimitrov - avatar
+ 1
If you had something like this: int x= 3; cout << ++x + x++; // Result would be 8, because in the time of printing, the last incrementation (postfix x++) will just wait, until operation is done. Afterwards, it increments value. So... int x= 3; x = ++x + x++ is 9 because: 1) int x = 3; 2) You assign value of (++x + x++) to x, and it's 8, because you increment x value (++x == 4) and add current value of x (4). 3) NOW you increment x value, because in previous step, you had x++, so it is incrementing x AFTER returning its value. :-)
22nd Jan 2017, 1:11 AM
Heroes Killer
Heroes Killer - avatar
+ 1
@Heroes Killer I don't think they meant "incorrect" as in you can't do it, but rather "undefined" with regard to the standard, meaning that implementations of what you get with it may differ between different compilers. But that part aside, from looking at the standard it seems there IS a defined behavior for it now, the wording is just confusing. (Also maybe I interpreted it wrong.) @Leon That's pretty interesting that you don't even need a prefix ++ going on in there for the behavior to change. Check this out: int x = 3, y = 3, z = 3; cout << x + x++; // 7 cout << y + z++; // 6
22nd Jan 2017, 2:58 AM
Potto
Potto - avatar
+ 1
it seems that this derives from unary increment operator is executed before sum operator . rhs x is incremented after its value is used for the sum, but lhs x is incremented before the sum as a result of the rhs post increment. so: x + x++ is 4 + 3
23rd Jan 2017, 6:02 AM
ifl
ifl - avatar
0
@Heroes Killer The result of int x = 3; cout << ++x + x++; is also 9, though.
22nd Jan 2017, 1:16 AM
Potto
Potto - avatar
0
I'll post another answer later, now I am on phone, so briefly. :) x = ++x + x++; is absolutely correct statement in C++, it is like (x = (x+1) + x), then + 1. However, what is NOT defined, is something like ++x++, which is an undefined expression.
22nd Jan 2017, 2:32 AM
Heroes Killer
Heroes Killer - avatar