ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
int a = 5, b = 2, c;
c = a > b ? a++ : --b;
printf("%d %d %d", a, b, c);
  1. A. 6 2 5
  2. B. 5 1 1
  3. C. 6 2 6
  4. D. 5 2 5

Correct answer: A. 6 2 5

The ternary operator fully sequences evaluation of its condition before evaluating EXACTLY ONE of the two branches (never both). a > b (5 > 2) is true, so only a++ runs: c gets a's OLD value, 5 (post-increment), and a becomes 6. b is never touched since its branch (--b) never executes. Output: 6 2 5.

More C Programming questions

Exams that ask this topic