És miért is? Ezt írja egy szabvány a side-effectes kifejezések kiértékeléséről:
"6.5 Expressions
1 An expression is a sequence of operators and operands that specifies computation of a
value, or that designates an object or a function, or that generates side effects, or that
performs a combination thereof. The value computations of the operands of an operator
are sequenced before the value computation of the result of the operator.
2 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. If there are multiple allowable orderings of the
subexpressions of an expression, the behavior is undefined if such an unsequenced side
effect occurs in any of the orderings."
És az == kiértékeléséről nincs megadva, hogy milyen sorrendet kell követnie, viszont a post-increment operatorról elő van írva, hogy:
"The value computation of the result is sequenced before the side effect of updating the stored value of the operand."
Azaz előbb kell kiszámolni a kifejezés értékét, és csak utána (valamikor) kell frissíteni az operandus értékét.
Azaz előfordulhat (és teljesen szabályos) a következő kiértékelés:
1. Kiszámoljuk C+1 értékét (C++ kiértékelésekor)
2. Összehasonlítjük C értékével
3. Mivel nem egyeznek, az egyenlőségvizsgálat értéke 0 lesz.
4. Ezután C-t megnöveljük 1-gyel.
Mint ahogy teljesen szabályos a következő kiértékelés is:
1. Kiszámoljuk C+1 értékét (C++ kiértékelésekor)
2. Megnöveljük C-t 1-gyel
2. Összehasonlítjük az 1-es pontban kiszámolt értéket C értékével
3. Mivel egyeznek, az egyenlőségvizsgálat értéke 1 lesz.
Ugyanígy, a szabvány leírja, hogy például az i = 1 + i++ kifejezés kiértékelése undefined.
Azt már csak futólag említem, hogy C-ben az egyenlőségvizsgálatok értéke sosem true és false, hanem 1 és 0.
"The == (equal to) and != (not equal to) operators are analogous to the relational
operators except for their lower precedence. 108) Each of the operators yields 1 if the
specified relation is true and 0 if it is false. The result has type int"