ProgrammingHard
int r = strcmp("abc", "abd"); What can be said about r according to the C standard?
- A. It is exactly -1
- B. It is zero, since only the first two letters are compared
- C. It is negative, since 'c' is less than 'd'
- D. It is positive, since the strings have equal length
Correct answer: C. It is negative, since 'c' is less than 'd'
The standard guarantees only the sign of strcmp's result. The first differing characters are c and d, so the sign is negative.
More C Programming questions
- const char s = "abcabc"; printf("%ld", (long)(strstr(s, "ca") - s)); What is printed?
- char d[4] = "abc"; printf("%zu", sizeof d - strlen(d)); What is printed?
- char s[] = "Hello"; for (char p = s; p; p++) if (p >= 'a' && p <= 'z') p -= 32; printf("%s", s); What is printed?
- int a, b; int n = sscanf("12-34", "%d-%d", &a, &b); printf("%d", b); What is printed?
- char s[10], w[10]; int n = 0; sscanf("abc123", "%[a-z]%d", s, &n); printf("%d", n); What is printed?
- char s[] = "a b,c"; int count = 0; for (char t = strtok(s, " ,"); t; t = strtok(NULL, " ,")) count++; printf("%d", count); What is printed?