ProgrammingHardTCS NQT · Product company OA

What is printed?

What is printed?
if (strcmp("abc", "abc")) puts("same");
else puts("different");
  1. A. Nothing
  2. B. Compilation error
  3. C. same
  4. D. different

Correct answer: D. different

strcmp returns 0 when the strings are equal, and 0 is false, so the else branch runs and prints the misleading word "different". The correct test is strcmp(a, b) == 0.

More C Programming questions

Exams that ask this topic