ProgrammingHardTCS NQT · Infosys pattern

Assume 32-bit int. What is printed?

Assume 32-bit int. What is printed?
int x = 3;
int result = x++ + x++ - x++;
printf("%d", result);
printf(" valid: %s", "no, undefined behaviour");
  1. A. The numeric result shown by result is not something any particular value can be trusted to equal, since x is modified three times with no sequence points between them
  2. B. 3
  3. C. Compile-time error
  4. D. 9

Correct answer: A. The numeric result shown by result is not something any particular value can be trusted to equal, since x is modified three times with no sequence points between them

x++ + x++ - x++ performs three separate increment side effects on the same object x within one expression, with no sequence points separating them — classic undefined behaviour, regardless of whatever a given compiler happens to print for result.

More C Programming questions

Exams that ask this topic