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 the C Standard. Counterintuitively, unspecified behavior in behavior for whech which the standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance. Consequently, unspecified behavior can be a portability issue because 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 sequences may in fact be executed in a different order.

...

This recommendation is related to EXP30-C. Do not depend on the order of evaluation for side effects, but it focuses on behavior that is nonportable or potentially confusing.

...

Consequently, the result of the following this noncompliant code example depends on unspecified behavior:

Code Block
bgColor#FFcccc
langc
#include <stdio.h>

int g;

int f(int i) {
  g = i;
  return i;
}

int main(void) {
  int x = f(1) + f(2);
  printf("g = %d\n", g);
  /* ... */
  return 0;
}

...