ProgrammingHard

int n = snprintf(buf, sizeof buf, "%s", text); if (n >= (int)sizeof buf) puts("truncated"); What does the test detect?

  1. A. Whether the output was cut short, since n is the length the full text would need
  2. B. Whether buf holds a NULL pointer
  3. C. Whether text contains a newline
  4. D. Whether the buffer was allocated with malloc

Correct answer: A. Whether the output was cut short, since n is the length the full text would need

snprintf returns the length the full output would have had. A value at least as large as the buffer means truncation occurred.

More C Programming questions

Exams that ask this topic