Undefined behavior in C

121 Views Asked by At

In this website, in the last section, they have provided f(i = -1, i = -1) as an example of undefined behavior due to unsequenced evaluation of subexpressions within function arguments. But since there is a sequence point after the evaluation of all function arguments and of the function designator, and before the actual function call, f will always be called with (-1, -1) and i will be assigned -1. Is there any possibility of this not happening?

2

There are 2 best solutions below

2
bolov On BEST ANSWER

It's undefined behavior because the standard says it. Modifying a variable without a sequence point between the modifications is UB. There is no "unless both modifications set the same value" exception to the rule.

3
Lundin On

... there is a sequence point after the evaluation

Indeed. After the evaluation, so it does no good. The problem here is that there are two unsequenced side effects on i before the sequence point. It's formally UB.