ProgrammingModerate
char s[] = "abc"; printf("%c", s[strlen(s) - 1]); What is printed?
- A. b
- B. a
- C. d
- D. c
Correct answer: D. c
strlen("abc") is 3, so s[3 - 1] = s[2] is the last real character, 'c'; s[3] would be the terminator. 'b' subtracts one too many, and 'd' runs past the string.
More C Programming questions
- char w[] = "ab"; printf("%zu %zu", sizeof w, strlen(w)); What is printed (on any platform)?
- char a = "cat"; char b[] = "cat"; printf("%d", strcmp(a, b) == 0); What is printed?
- 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?