ProgrammingModerate

char d[4]; strncpy(d, "abcdef", 4); /* d now has no terminator */ Which call is safe to print d with?

  1. A. printf("%s", d);
  2. B. puts(d);
  3. C. printf("%.4s", d);
  4. D. printf("%zu", strlen(d));

Correct answer: C. printf("%.4s", d);

strncpy with n equal to the array size writes no terminator, so %s and strlen run past the array. A precision of 4 prints exactly the four bytes.

More C Programming questions

Exams that ask this topic