GATE Computer ScienceHardGATE CS (PYQ pattern)

What is the total number of characters written to the output by this program? (Assume arguments of the last printf are evaluated before the call.) int n = printf("%5d|%-4d|", 42, 7); int m = printf("%d", n); printf("%d", m + printf("%03d", n));

  1. A. 16
  2. B. 18
  3. C. 20
  4. D. 17

Correct answer: D. 17

First printf writes ' 42|7 |' = 11 chars and returns 11. printf("%d", 11) writes 2 and returns 2. Inner printf("%03d", 11) writes '011' (3) and returns 3, then the outer prints 2 + 3 = 5 (1 char). Total 11 + 2 + 3 + 1 = 17.

More Programming and Data Structures (GATE) questions

Exams that ask this topic