ProgrammingHard

A string of up to 10^6 characters is read with fgets. Which buffer declaration is correct?

  1. A. static char line[1000000];
  2. B. char line[10];
  3. C. static char line[1000001]; without any room for the newline check
  4. D. static char line[1000002];

Correct answer: D. static char line[1000002];

One byte is needed for the terminator and fgets may keep a newline, so 10^6 + 2 bytes is safe. Exactly 10^6 bytes leaves no room for the terminator of a full-length line.

More C Programming questions

Exams that ask this topic