ProgrammingModerate

char b[6]; snprintf(b, sizeof b, "%s-%d", "abcdef", 12345); int r = snprintf(NULL, 0, "%s-%d", "abcdef", 12345); printf("%s %d", b, r); What is printed?

  1. A. abcde 12
  2. B. abcdef 12
  3. C. abcde 5
  4. D. abcde 11

Correct answer: A. abcde 12

snprintf writes at most size-1 characters plus a terminator, so b holds 'abcde'. Its return value is the length the full output would have had, 'abcdef-12345' has 12 characters, whatever the truncation.

More C Programming questions

Exams that ask this topic