LearnFrenzy
Sign in Take a free test

C Input / Output · Find the Error · Question 7

Error: fgets(line, stdin) without a size Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

The program below has a bug. Which statement correctly describes it?

main.c
#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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links