ProgrammingVery hardProduct company OA

What is printed?

What is printed?
printf("%d %d", strncmp("abc", "abd", 2) == 0, strncmp("ab", "ab\0x", 5) == 0);
  1. A. 1 0
  2. B. 0 0
  3. C. 1 1
  4. D. 0 1

Correct answer: C. 1 1

strncmp compares at most n characters and also stops at a '\0'. The first compares only "ab" with "ab". In the second both strings end after "ab" (the literal's '\0' hides the x), so they compare equal even though n is 5.

More C Programming questions

Exams that ask this topic