liveBook · Manning

Fflush

As explained in Modern C

fflush is the C standard library function that flushes buffered output for a stream without necessarily closing it.

Consider a program that prints a message, waits, and then prints ten dots. Output to a text stream is usually buffered, so the characters may not appear immediately. fflush(stdout) requests that buffered output be written without closing stdout. section 8.3.3

Flush output while a program continues

#include <stdio.h> int main(int argc, char* argv[argc+1]) { fputs("waiting 10 seconds for you to stop me", stdout); if (argc < 3) fflush(stdout); for (unsigned i = 0; i < 10; ++i) { fputc('.', stdout); if (argc < 2) fflush(stdout); delay(1.0); } fputs("\n", stdout); } Here, flushing before the loop can make the initial message visible, while flushing after each dot makes the dots appear during the delay. Running the program with zero, one, and two command-line arguments demonstrates the different buffering behavior. section 8.3.3

Use fflush when:

  • Output must become visible immediately on a terminal. section 8.3.3
  • Written data must reach its destination before the stream is closed later. section 8.3.3
  • The stream should remain open; fclose also flushes buffers, but closes the stream. section 8.3.2 section 8.3.3
Listing 8.1. flushing buffered output
 1   #include <stdio.h>
 2
 3   /* delay execution with some crude code,
 4      should use thrd_sleep, once we have that*/
 5   void delay(double secs) {
 6     double const magic = 4E8;   // works just on my machine
 7     unsigned long long const nano = secs* magic;
 8     for (unsigned long volatile count = 0;
 9          count < nano;
10          ++count) {
11       /* nothing here */
12     }
13   }
14
15   int main(int argc, char* argv[argc+1]) {
16     fputs("waiting 10 seconds for you to stop me", stdout);
17     if (argc < 3) fflush(stdout);
18     for (unsigned i = 0; i < 10; ++i) {
19       fputc('.', stdout);
20       if (argc < 2) fflush(stdout);
21       delay(1.0);
22     }
23     fputs("\n", stdout);
24     fputs("You did ignore me, so bye bye\n", stdout);
25   }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":1},{\"line\":0,\"ch\":2}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":1}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":1}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":1}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":1}],[{\"line\":13,\"ch\":0},{\"line\":13,\"ch\":1}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":1}],[{\"line\":15,\"ch\":0},{\"line\":15,\"ch\":1}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":1}],[{\"line\":17,\"ch\":0},{\"line\":17,\"ch\":1}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":1}],[{\"line\":20,\"ch\":1},{\"line\":20,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":13}],[{\"line\":1,\"ch\":1},{\"line\":1,\"ch\":2}],[{\"line\":11,\"ch\":1},{\"line\":11,\"ch\":2}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":1}],[{\"line\":20,\"ch\":0},{\"line\":20,\"ch\":1}],[{\"line\":21,\"ch\":0},{\"line\":21,\"ch\":1}],[{\"line\":22,\"ch\":0},{\"line\":22,\"ch\":1}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":1}],[{\"line\":24,\"ch\":0},{\"line\":24,\"ch\":1}],[{\"line\":2,\"ch\":1},{\"line\":2,\"ch\":2}],[{\"line\":12,\"ch\":1},{\"line\":12,\"ch\":2}],[{\"line\":16,\"ch\":18},{\"line\":16,\"ch\":19}],[{\"line\":22,\"ch\":1},{\"line\":22,\"ch\":2}],[{\"line\":3,\"ch\":1},{\"line\":3,\"ch\":2}],[{\"line\":5,\"ch\":28},{\"line\":5,\"ch\":29}],[{\"line\":13,\"ch\":1},{\"line\":13,\"ch\":2}],[{\"line\":23,\"ch\":1},{\"line\":23,\"ch\":2}],[{\"line\":4,\"ch\":1},{\"line\":4,\"ch\":2}],[{\"line\":14,\"ch\":1},{\"line\":14,\"ch\":2}],[{\"line\":24,\"ch\":1},{\"line\":24,\"ch\":2}],[{\"line\":4,\"ch\":5},{\"line\":4,\"ch\":9}],[{\"line\":4,\"ch\":16},{\"line\":4,\"ch\":22}],[{\"line\":5,\"ch\":7},{\"line\":5,\"ch\":13}],[{\"line\":5,\"ch\":1},{\"line\":5,\"ch\":2}],[{\"line\":15,\"ch\":1},{\"line\":15,\"ch\":2}],[{\"line\":5,\"ch\":7},{\"line\":5,\"ch\":19}],[{\"line\":6,\"ch\":1},{\"line\":6,\"ch\":2}],[{\"line\":16,\"ch\":1},{\"line\":16,\"ch\":2}],[{\"line\":6,\"ch\":7},{\"line\":6,\"ch\":31}],[{\"line\":5,\"ch\":30},{\"line\":5,\"ch\":31}],[{\"line\":7,\"ch\":1},{\"line\":7,\"ch\":2}],[{\"line\":17,\"ch\":1},{\"line\":17,\"ch\":2}],[{\"line\":7,\"ch\":7},{\"line\":7,\"ch\":10}],[{\"line\":15,\"ch\":33},{\"line\":15,\"ch\":36}],[{\"line\":17,\"ch\":7},{\"line\":17,\"ch\":10}],[{\"line\":7,\"ch\":12},{\"line\":7,\"ch\":34}],[{\"line\":8,\"ch\":1},{\"line\":8,\"ch\":2}],[{\"line\":18,\"ch\":1},{\"line\":18,\"ch\":2}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":2}],[{\"line\":15,\"ch\":22},{\"line\":15,\"ch\":24}],[{\"line\":17,\"ch\":32},{\"line\":17,\"ch\":34}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":2}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":2}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":2}],[{\"line\":13,\"ch\":0},{\"line\":13,\"ch\":2}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":2}],[{\"line\":14,\"ch\":5},{\"line\":14,\"ch\":8}],[{\"line\":14,\"ch\":9},{\"line\":14,\"ch\":13}],[{\"line\":14,\"ch\":5},{\"line\":14,\"ch\":8}],[{\"line\":14,\"ch\":24},{\"line\":14,\"ch\":28}],[{\"line\":15,\"ch\":0},{\"line\":15,\"ch\":2}],[{\"line\":15,\"ch\":7},{\"line\":15,\"ch\":12}],[{\"line\":22,\"ch\":7},{\"line\":22,\"ch\":12}],[{\"line\":23,\"ch\":7},{\"line\":23,\"ch\":12}],[{\"line\":15,\"ch\":54},{\"line\":15,\"ch\":60}],[{\"line\":16,\"ch\":28},{\"line\":16,\"ch\":34}],[{\"line\":18,\"ch\":20},{\"line\":18,\"ch\":26}],[{\"line\":19,\"ch\":30},{\"line\":19,\"ch\":36}],[{\"line\":22,\"ch\":19},{\"line\":22,\"ch\":25}],[{\"line\":23,\"ch\":48},{\"line\":23,\"ch\":54}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":2}],[{\"line\":16,\"ch\":7},{\"line\":16,\"ch\":9}],[{\"line\":19,\"ch\":9},{\"line\":19,\"ch\":11}],[{\"line\":16,\"ch\":21},{\"line\":16,\"ch\":27}],[{\"line\":19,\"ch\":23},{\"line\":19,\"ch\":29}],[{\"line\":15,\"ch\":54},{\"line\":15,\"ch\":60}],[{\"line\":16,\"ch\":28},{\"line\":16,\"ch\":34}],[{\"line\":18,\"ch\":20},{\"line\":18,\"ch\":26}],[{\"line\":19,\"ch\":30},{\"line\":19,\"ch\":36}],[{\"line\":22,\"ch\":19},{\"line\":22,\"ch\":25}],[{\"line\":23,\"ch\":48},{\"line\":23,\"ch\":54}],[{\"line\":17,\"ch\":0},{\"line\":17,\"ch\":2}],[{\"line\":7,\"ch\":7},{\"line\":7,\"ch\":10}],[{\"line\":15,\"ch\":33},{\"line\":15,\"ch\":36}],[{\"line\":17,\"ch\":7},{\"line\":17,\"ch\":10}],[{\"line\":6,\"ch\":7},{\"line\":6,\"ch\":15}],[{\"line\":7,\"ch\":12},{\"line\":7,\"ch\":20}],[{\"line\":17,\"ch\":12},{\"line\":17,\"ch\":20}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":2}],[{\"line\":18,\"ch\":9},{\"line\":18,\"ch\":14}],[{\"line\":15,\"ch\":54},{\"line\":15,\"ch\":60}],[{\"line\":16,\"ch\":28},{\"line\":16,\"ch\":34}],[{\"line\":18,\"ch\":20},{\"line\":18,\"ch\":26}],[{\"line\":19,\"ch\":30},{\"line\":19,\"ch\":36}],[{\"line\":22,\"ch\":19},{\"line\":22,\"ch\":25}],[{\"line\":23,\"ch\":48},{\"line\":23,\"ch\":54}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":2}],[{\"line\":16,\"ch\":7},{\"line\":16,\"ch\":9}],[{\"line\":19,\"ch\":9},{\"line\":19,\"ch\":11}],[{\"line\":16,\"ch\":21},{\"line\":16,\"ch\":27}],[{\"line\":19,\"ch\":23},{\"line\":19,\"ch\":29}],[{\"line\":15,\"ch\":54},{\"line\":15,\"ch\":60}],[{\"line\":16,\"ch\":28},{\"line\":16,\"ch\":34}],[{\"line\":18,\"ch\":20},{\"line\":18,\"ch\":26}],[{\"line\":19,\"ch\":30},{\"line\":19,\"ch\":36}],[{\"line\":22,\"ch\":19},{\"line\":22,\"ch\":25}],[{\"line\":23,\"ch\":48},{\"line\":23,\"ch\":54}],[{\"line\":20,\"ch\":0},{\"line\":20,\"ch\":2}],[{\"line\":21,\"ch\":0},{\"line\":21,\"ch\":2}],[{\"line\":22,\"ch\":0},{\"line\":22,\"ch\":2}],[{\"line\":15,\"ch\":7},{\"line\":15,\"ch\":12}],[{\"line\":22,\"ch\":7},{\"line\":22,\"ch\":12}],[{\"line\":23,\"ch\":7},{\"line\":23,\"ch\":12}],[{\"line\":15,\"ch\":54},{\"line\":15,\"ch\":60}],[{\"line\":16,\"ch\":28},{\"line\":16,\"ch\":34}],[{\"line\":18,\"ch\":20},{\"line\":18,\"ch\":26}],[{\"line\":19,\"ch\":30},{\"line\":19,\"ch\":36}],[{\"line\":22,\"ch\":19},{\"line\":22,\"ch\":25}],[{\"line\":23,\"ch\":48},{\"line\":23,\"ch\":54}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}],[{\"line\":15,\"ch\":7},{\"line\":15,\"ch\":12}],[{\"line\":22,\"ch\":7},{\"line\":22,\"ch\":12}],[{\"line\":23,\"ch\":7},{\"line\":23,\"ch\":12}],[{\"line\":15,\"ch\":54},{\"line\":15,\"ch\":60}],[{\"line\":16,\"ch\":28},{\"line\":16,\"ch\":34}],[{\"line\":18,\"ch\":20},{\"line\":18,\"ch\":26}],[{\"line\":19,\"ch\":30},{\"line\":19,\"ch\":36}],[{\"line\":22,\"ch\":19},{\"line\":22,\"ch\":25}],[{\"line\":23,\"ch\":48},{\"line\":23,\"ch\":54}],[{\"line\":24,\"ch\":0},{\"line\":24,\"ch\":2}]]"}
!@%STYLE%@!

Inspect the complete flushing example, including its delayed loop and the conditions that control when stdout is flushed. section 8.3.3

How Modern C covers it

How deep it goes
Explains buffering, line-buffered behavior, and a timed example, but defers broader stream and text-conversion details.
Examples
Uses stdout, fputs, fputc, delays, and command-line argument counts to demonstrate conditional flushing.
What it assumes
Assumes familiarity with C streams and output functions while explaining buffering through observable terminal behavior.
What you can do after
Apply fflush to expose buffered terminal output or deliver written stream content before closing it.
How it connects
Connects fflush with streams, stdout, text I/O, buffering, fclose, and line buffering.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.