ProgrammingModerate
Which output method is fast enough for 10^6 lines of output in C?
- A. Call fflush(stdout) after every printf
- B. Use system("echo") for each line
- C. Build the lines in a large buffer and write them with one fwrite, or use printf with stdout fully buffered
- 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
- Which 2D DP table declaration is correct for a 1000 x 1000 table of ints in a contest C program?
- A modular sum over 10^6 terms uses sum = (sum + a[i]) % MOD with a[i] < MOD and sum an int. What is wrong?
- A counting loop uses the expression i i <= n with int i and n up to 2^31 - 1. Is the loop correct?
- To read a whole line of unknown length in standard C without overflow, which approach is correct?
- A program sums the digits of 10^5 numbers with one loop per number that divides by 10. What is the total cost, about?
- A queue for BFS on a grid of 10^6 cells uses a fixed array of size 10^6 indexed by head and tail. Is this enough?