ProgrammingModerateTCS NQT · Infosys pattern

What is printed?

What is printed?
char a[] = "hi", b[] = "hi";
if (a == b) printf("same");
else printf("different");
  1. A. Compilation error
  2. B. Undefined behaviour
  3. C. same
  4. D. different

Correct answer: D. different

In the comparison both arrays decay to pointers to their first elements; a and b are two separate arrays, so the addresses differ and it prints different. Comparing contents needs strcmp(a, b) == 0.

More C Programming questions

Exams that ask this topic