Flushing the output buffers:

printf("Buffered, will be flushed");
fflush(stdout); // Prints to screen or whatever your standard out is

or

fprintf(fd, "Buffered, will be flushed");
fflush(fd);  //Prints to a file

Can be a very helpful technique. Why would you want to flush an output buffer? Usually when I do it, it's because the code is crashing and I'm trying to debug something. The standard buffer will not print everytime you call printf() it waits until it's full then dumps a bunch at once. So if you're trying to check if you're making it to a function call before a crash, it's helpful to printf something like "got here!", and sometimes the buffer hasn't been flushed before the crash happens and you can't tell how far you've really gotten.

Another time that it's helpful, is in multi-process or multi-thread code. Again, the buffer doesn't always flush on a call to a printf(), so if you want to know the true order of execution of multiple processes you should fflush the buffer after every print.

I make a habit to do it, it saves me a lot of headache in debugging. The only downside I can think of to doing so is that printf() is an expensive operation (which is why it doesn't by default flush the buffer).


As far as flushing the input buffer (stdin), you should not do that. Flushing stdin is undefined behavior according to the C11 standard §7.21.5.2 part 2:

If stream points to an output stream ... the fflush function causes any unwritten data for that stream ... to be written to the file; otherwise, the behavior is undefined.

On some systems, Linux being one as you can see in the man page for fflush(), there's a defined behavior but it's system dependent so your code will not be portable.

Now if you're worried about garbage "stuck" in the input buffer you can use fpurge() on that. See here for more on fflush() and fpurge()

Answer from Mike on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › use-fflushstdin-c
Use of fflush(stdin) in C - GeeksforGeeks
September 15, 2023 - fflush() is typically used for output stream only. Its purpose is to clear (or flush) the output buffer and move the buffered data to console (in case of stdout) or disk (in case of file output stream).
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › buffer-flush-means-c
What does buffer flush means in C++ ? - GeeksforGeeks
May 20, 2024 - Thus when we save our work, the ... to force the buffer to be written. Generally, the std::endl function works the same by inserting a new-line character and flushes the stream....
Discussions

Flushing buffers in C - Stack Overflow
Should fflush() not be used to flush a buffer even if it is an output stream? What is it useful for? How do we flush a buffer in general? More on stackoverflow.com
🌐 stackoverflow.com
[C++] What does flushing the buffer means ?
Imagine you're job is to change the marque just under the sign of a fast food restaurant. The message you want to display is "Open 24 hours". Here's one way to do it. Get a ladder Get a box of letters Find the capital O Climb the ladder Place the O Climb down the ladder Get the next letter Repeat until done No intelligent human would take these steps. It's too much work. But a computer is a slave to the program and will follow instructions with no complaints. So how would an intelligent human solve this problem? Here's how: Get a ladder Get a box of letters Get a bag Find the capital O Put it in the bag Find the next letter and repeat until all the letters are in the bag Climb the ladder Find the capital O Place the O Repeat placing each letter until finished Climb down the letter Notice how much less effort you had to exert by first putting all of the letters into the bag. That's because the work necessary to move the letters from the ground to the sign was only done once. Displaying a message in a computer is similar. While you may write a simple line of code to display a message, 1000s or 100,000s of lines of code are executed just to display your message. This is a lot of work. So for efficiency, your program's output is placed in a buffer. It's like our bag example, except it's more efficient since it's ordered. The first character that you want to display is the first thing in the buffer and so on. In the example with the bag, you had to find the letters all over again. Flushing a buffer is like emptying the bag. If you climb the ladder and don't empty the bag, your sign will say nothing. If your program exits WITHOUT flushing the buffer, then the data is never displayed. BTW, buffers are used everywhere. Network communications is one notable place. Imagine how much overhead it would be to write your friend a letter if you wrote one word on a piece of paper and then mailed that letter. Then wrote the next word on the next sheet and mailed that letter. We don't do that because it's inefficient. Instead we write all the words onto one or more sheets of paper and then mail them once. In network communications, we buffer the data we want to send until the buffer is full at which point it's automatically flushed or we have no more to send and we manually flush the buffer. Hope this helps. More on reddit.com
🌐 r/learnprogramming
10
6
December 20, 2014
[C] Can someone please explain fflush(stdin) to me?
understand that this command flushes out the input stream No. Calling fflush() on stdin is undefined in C - it could do anything. "flushing" means removing everything from a buffer. In the case of the stdout output stream (for which calling fflush() is defined), then whatever is in the streams internal buffers (private memory) will be sent to the actual output device - if this is the screen, then whatever was in the buffer will be displayed. For alternative ways of clearing the input stream buffer, see http://c-faq.com/stdio/stdinflush2.html . More on reddit.com
🌐 r/learnprogramming
10
9
July 1, 2015
What does "flushing the output buffer" mean in CPP ?
A lot of I/O is buffered. This means that data is not immediately sent to the target; instead it is stored in a buffer and sent only when explicitly requested or when the buffer is full. Sending data in large chunks slightly delays it but is very optimal in terms of time spend for system/driver calls. More on reddit.com
🌐 r/cpp_questions
9
13
September 17, 2020
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › clearing-the-input-buffer-in-cc
Clearing The Input Buffer In C/C++ - GeeksforGeeks
July 23, 2025 - 1. Using “ while ((getchar()) != '\n'); ”: Typing “while ((getchar()) != '\n');” reads the buffer characters till the end and discards them(including newline) and using it after the “scanf()” statement clears the input buffer and ...
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_fflush.htm
C Library - fflush() function
The C library fflush() function flushes the output buffer of a stream.This function forces a write of all buffered data for the given output or update stream to the file. When applied to an input stream, its behavior is undefined.
Top answer
1 of 1
133

Flushing the output buffers:

printf("Buffered, will be flushed");
fflush(stdout); // Prints to screen or whatever your standard out is

or

fprintf(fd, "Buffered, will be flushed");
fflush(fd);  //Prints to a file

Can be a very helpful technique. Why would you want to flush an output buffer? Usually when I do it, it's because the code is crashing and I'm trying to debug something. The standard buffer will not print everytime you call printf() it waits until it's full then dumps a bunch at once. So if you're trying to check if you're making it to a function call before a crash, it's helpful to printf something like "got here!", and sometimes the buffer hasn't been flushed before the crash happens and you can't tell how far you've really gotten.

Another time that it's helpful, is in multi-process or multi-thread code. Again, the buffer doesn't always flush on a call to a printf(), so if you want to know the true order of execution of multiple processes you should fflush the buffer after every print.

I make a habit to do it, it saves me a lot of headache in debugging. The only downside I can think of to doing so is that printf() is an expensive operation (which is why it doesn't by default flush the buffer).


As far as flushing the input buffer (stdin), you should not do that. Flushing stdin is undefined behavior according to the C11 standard §7.21.5.2 part 2:

If stream points to an output stream ... the fflush function causes any unwritten data for that stream ... to be written to the file; otherwise, the behavior is undefined.

On some systems, Linux being one as you can see in the man page for fflush(), there's a defined behavior but it's system dependent so your code will not be portable.

Now if you're worried about garbage "stuck" in the input buffer you can use fpurge() on that. See here for more on fflush() and fpurge()

🌐
GeeksforGeeks
origin.geeksforgeeks.org › buffer-flush-means-c
What does buffer flush means in C++ ? - GeeksforGeeks
May 20, 2024 - Thus when we save our work, the ... to force the buffer to be written. Generally, the std::endl function works the same by inserting a new-line character and flushes the stream....
🌐
Scaler
scaler.com › home › topics › fflush() in c
fflush() in C - Scaler Topics
May 4, 2023 - The function fflush(stdin) is used to flush the output buffer of the stream. It returns zero, if successful otherwise, returns EOF and of error, the indicator is set.
Find elsewhere
🌐
O'Reilly
oreilly.com › library › view › c-in-a › 0596006977 › re73.html
fflush - C in a Nutshell [Book]
December 16, 2005 - NamefflushSynopsisClears a file buffer#include <stdio.h> intfflush( FILE *fp );The fflush() function empties the I/O buffer of the open file specified by the FILE pointer... - Selection from C in a Nutshell [Book]
Authors   Peter PrinzTony Crawford
Published   2005
Pages   618
🌐
Educative
educative.io › answers › what-is-fflush-in-c
What is fflush in C?
The fflush function in C is used to immediately flush out the contents of an output stream.
🌐
Quora
quora.com › What-is-the-use-of-the-fflush-in-the-c-language
What is the use of the fflush() in the c language? - Quora
Summary fflush() is a tool to control when buffered output is actually written out. Use it to guarantee timely visibility of output or to manage buffering behavior; avoid fflush(stdin) and remember it doesn’t replace OS-level syncing (fsync) ...
🌐
GNU
gnu.org › software › libc › manual › html_node › Flushing-Buffers.html
Flushing Buffers (The GNU C Library)
If you want to flush the buffered output at another time, call fflush, which is declared in the header file stdio.h.
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › stdio_h › fflush.php
C Language: fflush function (Flush File Buffer)
In the C Programming Language, the fflush function writes any unwritten data in stream's buffer. If stream is a null pointer, the fflush function will flush all streams with unwritten data in the buffer.
🌐
Sanfoundry
sanfoundry.com › c-tutorials-flushing-seeking-functions-uses-program
fflush() and fseek() Functions in C - Sanfoundry
December 31, 2025 - This C program creates a file called sanfoundry_log.txt and writes log messages to it. It writes the first message and immediately flushes the buffer using fflush(certification) so the data saves right away.
🌐
Reddit
reddit.com › r/learnprogramming › [c++] what does flushing the buffer means ?
r/learnprogramming on Reddit: [C++] What does flushing the buffer means ?
December 20, 2014 -

Hi, I am learning C++ . What does flushing the buffer means ? with respect to cin and cout?

Can anyone explain like i'm five Please?

Top answer
1 of 2
4
Imagine you're job is to change the marque just under the sign of a fast food restaurant. The message you want to display is "Open 24 hours". Here's one way to do it. Get a ladder Get a box of letters Find the capital O Climb the ladder Place the O Climb down the ladder Get the next letter Repeat until done No intelligent human would take these steps. It's too much work. But a computer is a slave to the program and will follow instructions with no complaints. So how would an intelligent human solve this problem? Here's how: Get a ladder Get a box of letters Get a bag Find the capital O Put it in the bag Find the next letter and repeat until all the letters are in the bag Climb the ladder Find the capital O Place the O Repeat placing each letter until finished Climb down the letter Notice how much less effort you had to exert by first putting all of the letters into the bag. That's because the work necessary to move the letters from the ground to the sign was only done once. Displaying a message in a computer is similar. While you may write a simple line of code to display a message, 1000s or 100,000s of lines of code are executed just to display your message. This is a lot of work. So for efficiency, your program's output is placed in a buffer. It's like our bag example, except it's more efficient since it's ordered. The first character that you want to display is the first thing in the buffer and so on. In the example with the bag, you had to find the letters all over again. Flushing a buffer is like emptying the bag. If you climb the ladder and don't empty the bag, your sign will say nothing. If your program exits WITHOUT flushing the buffer, then the data is never displayed. BTW, buffers are used everywhere. Network communications is one notable place. Imagine how much overhead it would be to write your friend a letter if you wrote one word on a piece of paper and then mailed that letter. Then wrote the next word on the next sheet and mailed that letter. We don't do that because it's inefficient. Instead we write all the words onto one or more sheets of paper and then mail them once. In network communications, we buffer the data we want to send until the buffer is full at which point it's automatically flushed or we have no more to send and we manually flush the buffer. Hope this helps.
2 of 2
2
You print some text, it isn't showing in the terminal. Why isn't it showing? The buffer hasn't been flushed? Why is stdio buffered? Because printing characters is an expensive operation so you want to batch it. How do I flush it ? std::cout<
🌐
George Washington University
www2.seas.gwu.edu › ~simhaweb › C › modules › appendix › misc › misc.html
Using fflush() to force output
int main () { int A[10]; int *B; int i; printf ("Before first for-loop\n"); fflush (stdout); for (i=0; i<10; i++) { A[i] = 1; } printf ("After first for-loop\n"); fflush (stdout); for (i=0; i<10; i++) { B[i] = A[i]; } printf ("After second for-loop\n"); fflush (stdout); } Upon compilation and execution, we see the following output: % gcc -o flush flush.c % flush Before first for-loop After first for-loop Segmentation fault (core dumped) Now we know the seg-fault occurs in the second loop.
🌐
ZetCode
zetcode.com › clang › fflush
C fflush Tutorial: Mastering Output Buffering with Practical Examples
The fflush function in C forces any buffered output data to be written immediately to the associated output stream. It takes a single parameter: a FILE pointer to the stream you want to flush. For output streams, fflush writes any unwritten data. For input streams, the behavior is ...
🌐
Fresh2Refresh
fresh2refresh.com › home › c programming tutorial › c – file handling › fflush() function in c
fflush() function in C | C File Handling | Fresh2Refresh
September 22, 2020 - fflush() function is used to flush a file or buffer. i.e. it cleans it (making empty) if it has been loaded with any other data already.
🌐
Reddit
reddit.com › r/learnprogramming › [c] can someone please explain fflush(stdin) to me?
r/learnprogramming on Reddit: [C] Can someone please explain fflush(stdin) to me?
July 1, 2015 -

I understand that this command flushes out the input stream but what does it mean by "flushing the stream".

🌐
Quora
quora.com › Why-do-we-use-the-functions-fflush-stdin-and-fflush-stdout-in-c
Why do we use the functions fflush(stdin) and fflush(stdout) in c? - Quora
Answer (1 of 6): Let us first understand the different I/O functions that the standard library provides and their relationship to each other. Output For formatted output, you have fprintf / printf / and their variants. For string output, you have fputs. For output of uninterpreted data i.e. raw ...