ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
int x = 5;
int y = (x = 10) + (x = 20);
printf("%d", y);
  1. A. 30
  2. B. 20
  3. C. Undefined behaviour: x is assigned twice in the same expression, unsequenced relative to each other
  4. D. 10

Correct answer: C. Undefined behaviour: x is assigned twice in the same expression, unsequenced relative to each other

Both (x = 10) and (x = 20) are side effects on the SAME object x, and the + operator does not sequence its two operand evaluations relative to each other. Two unsequenced writes to the same scalar is undefined behaviour, regardless of what any particular compiler happens to compute for y.

More C Programming questions

Exams that ask this topic