ProgrammingHardHCL · Tech Mahindra pattern

Assume 32-bit int. What is printed?

Assume 32-bit int. What is printed?
int x = 5, y = 3;
int result = x++ - ++y;
printf("%d %d %d", x, y, result);
  1. A. 6 4 1
  2. B. 5 3 2
  3. C. 6 4 2
  4. D. 5 4 1

Correct answer: A. 6 4 1

x++ yields x's current value, 5, for the subtraction, then increments x to 6. ++y increments y to 4 first, then yields that new value, 4, for the subtraction. result = 5 - 4 = 1. Since the two side effects act on DIFFERENT variables (x and y), there's no undefined behaviour here, just two independent, well-defined increments.

More C Programming questions

Exams that ask this topic