The behavior of code that has both x == 35
and x = 50
in the arguments to a function is not defined by the C standard, because it both modifies x
and separately uses its value, and the rules of C does not say which happens first. C 2018 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.
In x = 50
, the main value is 50, and the side effect is to change the value stored in x
to 50.
In x == 35
, the expression is a value computation that uses the value of x
.
In general, evaluations of subexpressions are unsequenced, because C 2018 6.5 3 says:
Except as specified later, side effects and value computations of subexpressions are unsequenced
C 2018 6.5.2.2 specifies what happens with function calls, and it does not specify the order in which arguments are evaluated. So 6.5 3, quoted above, applies. x = 50
and x == 35
are unsequenced. So there is both a side effect on x
and a value computation of x
that are unsequenced, so 6.5 2 applies, and the behavior is not defined by the C standard.