Question 1
Back to Exercise
The program below has a problem. Which statement correctly describes it?
#include <stdio.h>
#include <string.h>
int main(void) {
char path[8] = "/usr";
strcat(path, "/local");
printf("%s\n", path);
return 0;
}
- A strcat() cannot append a string that starts with '/'
- B path needs 11 bytes ("/usr/local" + '\0') but has 8, so strcat() writes past the end Correct
- C path must be a pointer for strcat()
- D There is no error; it prints /usr/local
Correct Answer: Option B
strcat() trusts that the destination is big enough. "/usr/local" is 10 characters plus the terminator = 11 bytes, so 3 bytes land outside path. Use char path[16] or snprintf().
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!