ProgrammingHardIBM · Deloitte pattern

What happens when this runs?

What happens when this runs?
int a = 5;
int b = a++ + a++ + a++;
printf("%d %d", a, b);
  1. A. 8 18, deterministically
  2. B. Undefined behaviour: a is modified three times in one expression with no sequence points separating the increments
  3. C. 8 15, deterministically
  4. D. 15 8, deterministically

Correct answer: B. Undefined behaviour: a is modified three times in one expression with no sequence points separating the increments

a++ + a++ + a++ performs three separate increment side effects on the same object a within a single expression, with no sequence points between them (addition does not provide one) — this is undefined behaviour, regardless of what any particular compiler's evaluation order happens to compute.

More C Programming questions

Exams that ask this topic