ISO/IEC 9899(TC2) §6.5 — 2 Expressions tells us:
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.
This is what I kept in mind, remembered and would have told anyone asking me why the line from the title gives unexpected outputs.
But today I just discovered this line:
§7.19.6 — 1 Formatted input/output functions:
The formatted input/output functions shall behave as if there is a sequence point after the actions associated with each specifier.
Which made me assume:
While
int i = 0;
printf ("%d, %d", ++i, i++);
should be undefined, the next example should be ok by the mentioned clause:
int i = 0;
printf ("%d, %d, %d", ++i, i, i++);
But the output is:
2, 2, 0
I never had seen a better example for this indicating undefined behavior.
But why? If the clause is true
"[...]behave as if there is a sequence point after the actions associated with each specifier."
then applying the rule under §6.5 — 2 onto each of the actiosn associated with the specifier doesn't let us cross that rule as in:
(SP representing a relevant sequence point)
SP1++i
SP2i
SP3i++
From SP1 in the given range between the previous and the next SP, ++i
is the only modification of i
's stored value.
From SP2 what is in the range between the previous and the next SP is: ++i
and i
where ++i
still is the only modification of that value.
If we now take SP3 all that is going on between the previous SP (SP2) and the next SP(end of the invocation) is:
i
and i++
still just a single modification of i
in the whole range between the previous and the next SP.
So what am I interpreting here wrong about the way sequence points work?