ProgrammingHard
char *a = "cat"; char b[] = "cat"; printf("%d", strcmp(a, b) == 0); What is printed?
- A. 0
- B. 1
- C. 2
- D. 5
Correct answer: B. 1
Both strings hold the same characters, so strcmp returns 0 and the comparison with 0 is true. The == operator would compare addresses, which is a different test.
More C Programming questions
- char buf[3] = {'a', 'b', 'c'}; printf("%.2s", buf); What is printed?
- char s[10] = "xyz"; strcpy(s + 1, "ab"); printf("%s", s); What is printed?
- char s[] = "hello"; char p = s; while (p) { if (p == 'l') p = 'L'; p++; } printf("%s", s); What is printed?
- char s = "abc"; char t[4]; strcpy(t, s); t[1] = 'X'; printf("%s", t); What is printed?
- char buf[5] = "abcd"; buf[4] = 'e'; printf("%.5s", buf); What is printed (no terminator is present, which is defined)?
- char s[] = "AbC"; for (char p = s; p; p++) p = isupper((unsigned char)p) ? tolower((unsigned char)p) : toupper((unsigned char)p)…