ProgrammingHardTCS NQT · Product company OA
What is printed?
What is printed?
if (strcmp("abc", "abc")) puts("same");
else puts("different");- A. Nothing
- B. Compilation error
- C. same
- 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
- Assume ASCII. What is printed? c int x = 'A' + 1.5; printf("%c %d", x, x);
- Given int m[3][4];, why can m not be passed to a function declared void f(int p);?
- Assume C99 or later. What is printed? c void f(int n, int a[][n]) { printf("%zu", sizeof a[0] / sizeof a[0][0]); } int main(void) { int…
- What is printed? c int c = 0; for (int i = 1; i <= 10; i++) for (int j = i; i j <= 10; j++) c++; printf("%d", c);
- What does z("abcdef") print? c void z(const char s) { if (!s) return; putchar(s); z(s + 2); if (s[1]) putchar(s[1]); }
- Does this function compile, and what does it do? c struct S { int p; }; void f(const struct S s) { s->p = 5; }