Weird post/pre increment equation

Why does this equation output a 5?

1
2
n = 1;
int n2 = ++n + --n - ++n;


doesnt differ if i cout it directly or put it as a variable it always outputs 5.

I expect it to output, a 1 ?
Last edited on
I expect it to output...

Your assumption is that that the side effects occur in a well-defined order (left-to-right). They don't. That statement causes undefined behavior.
Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced. [...] If a side effect on a memory location is unsequenced relative to either another side effect on the same memory location or a value computation using the value of any object in the same memory location, and they are not potentially concurrent, the behavior is undefined.
http://eel.is/c++draft/intro.execution#17
Last edited on
Topic archived. No new replies allowed.