ProgrammingHard
char *s = "x"; char t[2] = {'y', '\0'}; int r = strcmp(s, t); What does r look like?
- A. Negative, since 'x' comes before 'y'
- B. Positive, since 'x' is larger in ASCII
- C. Exactly 0, because both strings have one character
- D. Undefined, since t is not a literal
Correct answer: A. Negative, since 'x' comes before 'y'
The first characters are 'x' and 'y'. In ASCII 'x' is 120 and 'y' is 121, so the result is negative.
More C Programming questions
- printf("[%03d]", 7); What is printed?
- printf("[%5s|%-5s]", "ab", "cd"); What is printed?
- printf("%x %o", 255, 8); What is printed?
- char s[5] = "abcd"; printf("%s", s + 2); What is printed?
- char buf[4]; int n = snprintf(buf, sizeof buf, "%c%c%c%c", 'w', 'x', 'y', 'z'); printf("%d %s", n, buf); What is printed?
- char names[] = {"ab", "cde"}; printf("%zu", sizeof names / sizeof names[0]); What is printed?