Question 7
Back to Exercise
The program below has a bug. Which statement correctly describes it?
#include <stdio.h>
int main(void) {
char line[50];
if (fgets(line, stdin) != NULL)
fputs(line, stdout);
return 0;
}
- A fputs must be puts
- B fgets takes three arguments – buffer, size and stream – so the size is missing: fgets(line, sizeof line, stdin) Correct
- C line must be a pointer
- D There is no bug
Correct Answer: Option B
The size argument is what makes fgets safe. Use sizeof line so it always matches the array.
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!