ProgrammingModerate
char w[] = "ab"; printf("%zu %zu", sizeof w, strlen(w)); What is printed (on any platform)?
- A. 3 2
- B. 2 2
- C. 3 3
- D. 2 1
Correct answer: A. 3 2
sizeof counts the terminator too, so the array has 3 bytes. strlen counts only the characters.
More C Programming questions
- 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?
- char buf[5] = "abcd"; buf[4] = 'e'; printf("%.5s", buf); What is printed (no terminator is present, which is defined)?