ProgrammingHardInfosys · Capgemini

What is printed?

What is printed?
char s[10] = "abc";
char t[] = "xyz";
strncat(s, t, 2);
printf("%s %zu %d", s, strlen(s), strncmp(s, "abcx", 4) == 0);
  1. A. abcxyz 6 1
  2. B. abcxy 5 0
  3. C. abcxy 5 1
  4. D. abcx 4 1

Correct answer: C. abcxy 5 1

strncat appends at most 2 characters of t and always adds a '\0', giving "abcxy" (length 5). strncmp compares only the first 4 characters, "abcx" against "abcx", so it returns 0 and the test prints 1.

More C Programming questions

Exams that ask this topic