ProgrammingModerate

Which output method is fast enough for 10^6 lines of output in C?

  1. A. Call fflush(stdout) after every printf
  2. B. Use system("echo") for each line
  3. C. Build the lines in a large buffer and write them with one fwrite, or use printf with stdout fully buffered
  4. D. Open and close the output file for every line

Correct answer: C. Build the lines in a large buffer and write them with one fwrite, or use printf with stdout fully buffered

Full buffering keeps the per-line cost low. Flushing or reopening the file on every line adds a system call each time.

More C Programming questions

Exams that ask this topic