Question 4
Back to Exercise
The program below has a bug. Which statement correctly describes it?
#include <stdio.h>
#include <string.h>
int main(void) {
char code[3] = {'A', 'B', 'C'};
printf("%zu\n", strlen(code));
return 0;
}
- A strlen returns int
- B The array has no terminating '\0', so strlen reads past its end looking for one Correct
- C char arrays cannot be initialised with braces
- D There is no bug
Correct Answer: Option B
Every string function needs the terminator. Use char code[4] = "ABC";, or keep the length separately and use mem… functions.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!