ProgrammingModerateTCS NQT · Infosys

char buf[4]; int n = snprintf(buf, sizeof buf, "%d", 12345); What is n?

  1. A. 3
  2. B. 4
  3. C. 5
  4. D. 12345

Correct answer: C. 5

snprintf returns the length the full output would have had, which is 5, even though only three digits and the terminator fit in buf. Checking the return value is how you detect truncation.

More C Programming questions

Exams that ask this topic