Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

4
  • int b= a++ + a++; is undefined behaviour in C and C++, since a is modified twice without intervening sequence point (ditto for the int c = ... thing). Commented Apr 1, 2013 at 17:12
  • Yes you are right, I had also read that but I am just confuse and want to know how its behavior is undefined? Because in C/C++ if you also evaluate int a=2; and int b = a++ + a++ + a++ + a++; b's output will be same 8, it will take value and then do post increment of that operand after finishing of that statement. I hope you will get what I am trying to say. Commented Apr 2, 2013 at 7:24
  • Undefined means there's no restriction on what happens by the standard. It would not violate the standard if the compiler inserted an abort() call for the undefined behaviour. But of course no serious implementation does such things. Variations closer to the code include different orders of evaluation (right-to-left, middle-to-outer, invent-your-own) and the question of when the incremented value is stored, whether a is read only once or several times. So it would be legal for the value of a to be read three times, and the incremented value of a stored after evaluating the expression. Commented Apr 2, 2013 at 8:52
  • 1
    That could lead to int c = ++a + a++ + a++; being evaluated as 5 + 4 + 4, and then three times storing 4+1 into a, thus c == 13 and a == 5 afterward. Or the middle term evaluated first, and the incremented value not stored yet, then the ++a with immediate storing of the incremented value, then the right a++ with a new reading of the value, leading to c = 5 + 4 + 5 and a could be 5 or 6, depending on which pending store from the two a++ is done last. Commented Apr 2, 2013 at 8:57