ProgrammingHardProduct company OA

What is printed?

What is printed?
char d[6] = "zzzzz";
strncpy(d, "abc", 2);
printf("%s", d);
  1. A. abzzz
  2. B. abc
  3. C. abczz
  4. D. ab

Correct answer: A. abzzz

strncpy copies at most n = 2 characters and adds a '\0' only if the source ends within those n. Here it copies 'a' and 'b' and writes no terminator, so the old 'zzz' and the original '\0' remain. That missing terminator is the classic strncpy trap.

More C Programming questions

Exams that ask this topic