Versions Compared

Key

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

The order of evaluation of subexpressions, and the order in which side effects take place, are frequently defined as unspecified behavior by C99. Counter intuitively, unspecified behavior is where the standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance. Examples Consequently, unspecified behavior can be a portability issue, as different implementations can make different choices. If dynamic scheduling is used, however, there may not be a fixed code execution sequence over the life of a process. Operations that can be executed in different orderings, may in fact be executed in a different order.

According to C99, Section 6.5, "Expressions":

Except as specified later (for the function-call (), &&, ||, ?:, and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.

Specific examples of situations where the order of evaluation of subexpressions or the order in which side effects take place is unspecified include:

  • the order in which the arguments to a function are evaluated (C99, Section 6.5.2.2, "Function calls").
  • the order of evaluation of the operands in an assignment statement (C99, Section 6.5.16, "Assignment operators").
  • the order in which any side effects occur among the initialization list expressions is unspecified. In particular, the evaluation order need not be the same as the order of subobject initialization (C99, Section 6.7.8, "Initialization").

Unspecified behavior is generally a portability issue, as different implementations can make different choices. If dynamic scheduling is used, however, there may not be a fixed code execution sequence over the life of a process. Operations that can be executed in different orderings, may in fact be executed in a different order.

...

  • "

...

  • )

...

  • .

This recommendation is related to EXP30-C. Do not depend on order of evaluation between sequence points, but focuses on behavior that is non-portable or potentially confusing.

...