Does the following code invoke undefined behavior in C
?
int a = 1, b = 2;
a = b = (a + 1);
I know that the following does invoke UB:
a = b = a++;
The reason is that it violates the following clause from the standard:
Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.
However, the first snippet does not violate this clause. A coworker says that statement a = b = a+1
can mean either
a = a + 1;
b = a + 1;
or
b = a + 1;
a = b;
I think that due to the "right-to-left" associativity of =
, it always must mean a = (b = (a+1))
, not
a = a + 1;
b = a + 1;
I'm not positive, though. Is it UB?