Does the following code cause undefined behavior in C ?
int a = 1, b = 2; a = b = (a + 1);
I know the following calls UB:
a = b = a++;
The reason is that it violates the following sentence from the standard:
Between the previous and the next point in the sequence, the object must have its stored value, changed no more than once by evaluating the expression. In addition, the previous value should only be consulted to determine the value to be stored.
However, the first fragment does not violate this section. The employee says that the statement a = b = a+1 can mean either
a = a + 1; b = a + 1;
or
b = a + 1; a = b;
I think because of the “right to left” associativity = it should always mean a = (b = (a+1)) , not
a = a + 1; b = a + 1;
I am not sure, however. Is it UB?
source share