ProgrammingVery hard

int n = sprintf(NULL, "%s", "hello"); printf("%d", n); What is printed?

  1. A. UB, since sprintf must not write to a NULL pointer
  2. B. 5
  3. C. 0
  4. D. 6

Correct answer: A. UB, since sprintf must not write to a NULL pointer

sprintf writes to its first argument, so passing NULL is undefined. snprintf(NULL, 0, ...) is the defined way to measure the length.

More C Programming questions

Exams that ask this topic