ProgrammingHardTCS NQT · C

What is printed?

What is printed?
char s[20] = "abc";
strcat(s, "de");
strncpy(s + 1, "XYZW", 2);
printf("%s %zu", s, strlen(s));
  1. A. aXYde 7
  2. B. abXYe 5
  3. C. aXYZW 5
  4. D. aXYde 5

Correct answer: D. aXYde 5

strcat makes "abcde"; strncpy overwrites only 2 characters from index 1 ("XY") and, since n is less than the source length, adds no terminator; the length stays 5.

More C Programming questions

Exams that ask this topic