ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
int a = 3;
int result = a++ * 2 + --a;
printf("%d %d", a, result);
  1. A. 4 9
  2. B. 3 8
  3. C. 3 9
  4. D. Undefined behaviour: a is both incremented and decremented within the same expression without a defining sequence between those two side effects relative to their uses

Correct answer: D. Undefined behaviour: a is both incremented and decremented within the same expression without a defining sequence between those two side effects relative to their uses

a++ (post-increment) and --a (pre-decrement) both modify `a` within the same full expression, and the + operator doesn't sequence its two operands relative to each other; two unsequenced side effects on the same object is undefined behaviour, so no single 'correct' numeric trace exists for this program.

More C Programming questions

Exams that ask this topic