Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Non-compliant Code Example

In the following example, the order of evaluation of the operands to + is undefined.

Code Block

a = i + b[++i];

If i was equal to 0 before the statement, this statement may be result in the following outcome:

Code Block

a = 0 + b[1];

Or may also legally result in the following outcome:

Code Block

a = 1 + b[1];

As a result, programs can not safely rely on the order of evaluatoin of operands between sequence pionts.

Compliant Solution

The following examples are independend on the order of evaluation of the operands and can only be interpreted in one way.

Code Block

++i;
a = i + b[i];
Code Block

a = i + b[i+1];
++i;

References

C99 5.1.2.3 Program execution
C99 Annex C Sequence points