There is no rule in the C standard that states: "If the behavior is ambiguous, then the behavior is undefined." The actual rule in Release C of 1999 states: "Between the previous and next points in the sequence, an object must have a stored value no more than once, evaluating the expression. In addition, the previous value should only be read to determine the value that should be stored."
Your code violates this rule: it changes the value of a . (The note in 3.1-3 says that “Modify” includes the case where the new stored value matches the previous value.)
That's all. It doesn't matter if you can understand the unambiguous interpretation of this code. The fact is that it violates the rule. Since this violates the rule, the behavior is undefined.
In C 2011, this rule is indicated in a more technical way. 6.5 2 says: "If the side effect of a scalar object is independent of another side effect for the same scalar object or calculating a value using the value of the same scalar object, the behavior is undefined. If there are several valid ordering subexpressions of an expression, the behavior is undefined, if such a side effect does not have a side effect in any of the orderings. " When an assignment operator stores a value in an object, this is actually a side effect. (The main effect is that it evaluates the value that is saved.) Thus, this rule in C 2011 says basically the same as rule C 1999: you may not have two side effects for the same object.
source share