Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined behavior in C [duplicate]

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?

like image 578
Sourav Kannantha B Avatar asked May 11 '26 15:05

Sourav Kannantha B


2 Answers

... 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.

like image 192
Lundin Avatar answered May 13 '26 05:05

Lundin


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.

like image 35
bolov Avatar answered May 13 '26 04:05

bolov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!