ProgrammingModerateTCS NQT · Infosys pattern
What is printed?
What is printed?
char a[] = "hi", b[] = "hi";
if (a == b) printf("same");
else printf("different");- A. Compilation error
- B. Undefined behaviour
- C. same
- 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
- What is printed? c #define STR(x) #x #define CAT(a, b) a##b int xy = 7; printf("%s %d", STR(x+y), CAT(x, y));
- What is printed? c int x = 2; switch (x) { case 1: printf("A"); case 2: printf("B"); case 3: printf("C"); break; default: printf("D"); }
- What is printed? c int n = 5; while (n-- > 2) printf("%d", n); printf(" %d", n);
- Given int x = 1, y = 2; const int p = &x; int const q = &x; which pair of statements both fail to compile?
- What is printed? c enum { A, B = 5, C, D = 2, E }; printf("%d %d %d", A, C, E);
- With 4-byte int, what is printed? c int a[] = {1, 2, 3}; char p = (char )a; p += 4; printf("%d", (int )p);