Consider the C code a = a = a
. There\'s no sequence point for assignment, so this code produces a warning when compiling about an undefined operation on a
The C standard does not have a rule that says “If the behavior would be ambiguous, then the behavior is undefined.” The actual rule in C 1999 at issue says “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 read only to determine the value to be stored.”
Your code violates this rule: It modifies the value of a
. (The note at 3.1 3 says that “Modify” includes the case where the new value being stored is the same as the previous value.)
So that is it. It does not matter whether you can figure out an unambiguous interpretation for this code. It only matters that it violated the rule. Because it violated the rule, the behavior is undefined.
In C 2011, the rule is stated in a more technical way. 6.5 2 says “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.” When the assignment operator stores a value in an object, that is actually a side effect. (The main effect is that it evaluates to the value that is stored.) So this rule in C 2011 says largely the same thing as the C 1999 rule: You may not have two side effects on the same object.