ProgrammingHard
char *a = "ab", *b = "ab"; printf("%d", strcmp(a, b) == 0); What is printed?
- A. 0
- B. undefined
- C. 1
- D. 2
Correct answer: C. 1
strcmp compares the characters, which are equal here, so the result is 0 and the comparison with 0 is true. The == operator would compare addresses, which is not what this code does.
More C Programming questions
- char buf[8] = "12345"; buf[strlen(buf) - 1] = '0'; printf("%s", buf); What is printed?
- char s[] = "abc"; printf("%c", s[sizeof s - 1]); What is printed?
- printf("%d", (int)sizeof(char)); What is printed in C?
- char w[] = "one two"; char p = w; while (p && p != ' ') p++; printf("%d", (int)(p - w)); What is printed?
- char s[] = "aXbXc"; int x = 0; for (int i = 0; s[i]; i++) x += s[i] == 'X'; printf("%d", x); What is printed?
- printf("%s", strcat(strcpy(buf, "ab"), "cd")); with char buf[10] declared before the call. What is printed?