It doesn't invoke undefined behaviour. In 6.7.6 (3), it is stated
A full declarator is a declarator that is not part of another declarator. The end of a full
declarator is a sequence point.
that the end of a full declarator is a sequence point.
int a = 1, b = a++;
// ^ end of full declarator
The behavior is undefined, but it's not because of the modification of the same object twice between two sequence points but it is UB because the side effect on i
is unsequnced relative to the value computation of a[i-1]
and a[i]
using i
.
ยง6.5-p(2):
If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84)
For example, expression
a[i++] = i;
invokes undefined behavior. Same is true for
a[++i] = foo(a[i-1], a[i]);
Best Answer
Answer to your question
No.
What will happen
This will happen:
How it compiles
The same as:
Why this is not undefined
Because you are not writing
x
in the same expression you read it. You just set it to itself+ 1
, then assigny
to the value ofx
.Your future
If you find the code confusing you can use parentheses for readability: