The answer to this is that fflush(stream) is only formally defined for output streams, so fflush(stdout) is OK, but fflush(stdin) is not.

The purpose of fflush(stream) is to make the operating system flush any buffers to the underlying file. For an example of a legitimate use, students often have problems like “my prompt doesn't appear!” if they do something like:

printf("Enter a number: ");

However, they find that this works just fine:

printf("Enter a number:\n");

Of course, they don't want a newline after their prompt, so they have a bit of a problem.

The reason for this is that the output to stdout is buffered by the OS and the default behavior is (often) only to actually write the output to the terminal when a newline is encountered. Adding an fflush(stdout) after the printf() solves the problem:

printf("Enter a number: ");
fflush(stdout);

Now, working by analogy, people often think that fflush(stdin) should discard any unused input, but if you think about it a little bit that doesn't make much sense. What does it mean to “flush” an input buffer? Where is it “flushed” to? If you flush an output buffer, the output is sent to the underlying file or the terminal, where it would eventually wind up anyway, but where would input “eventually end up anyway”? There's no way of knowing! What should the behavior be if the input stream data comes from a file or a pipe or a socket? It isn't at all clear for input streams what the behavior of fflush() should be, but it's very clear for output streams in all cases. Hence, fflush() is only defined for output streams.

The reason why the erroneous use of fflush(stdin) became commonplace is that, many years ago, a few operating systems did implement a scheme where it worked as many people expected, discarding unused input. Microsoft DOS is a good example. Surprisingly, modern versions of Linux also implement fflush() for input streams.

The right thing to do with “extra” unwanted terminal input is simply to read it and do nothing with it. This is almost as easy as calling fflush(stdin), works everywhere, and doesn't rely on formally undefined behavior.

The C standard says:

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

POSIX says (also explicitly defers to C standard):

If stream points to an output stream or an update stream in which the most recent operation was not input, fflush() shall cause any unwritten data for that stream to be written to the file, ...

But the Linux manpage says:

For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function. For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application. The open status of the stream is unaffected.

Answer from Emmet on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_fflush.htm
C Library - fflush() function
#include <stdio.h> int main() { FILE *file = fopen("example1.txt", "w"); if (file == NULL) { perror("Failed to open file"); return 1; } fprintf(file, "Hello, World!\n"); // Ensure "Hello, World!" is written to the file immediately fflush(file); fclose(file); return 0; } The above code writes "Hello, World!" to a file and uses fflush to ensure the data is written to the disk immediately, instead of waiting for the file to close.−
🌐
Scaler
scaler.com › home › topics › fflush() in c
fflush() in C - Scaler Topics
May 4, 2023 - The return type of the fflush() function in C is zero if the function call is successful which means the buffer is flushed and if it is unsuccessful then it will return an error that is End of File or eof in form of a positive integer and an indicator is set to indicate feof. To learn about the errors like eof, feof, etc please check the article Error Handling in C. In this example, we are going to use the flush() function of the C programming language to print the data of buffered memory on the console without using the end-line character (\n).
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › use-fflushstdin-c
Use of fflush(stdin) in C - GeeksforGeeks
September 15, 2023 - As per C standard, it is undefined behavior to use fflush(stdin). However some compilers like Microsoft visual studio allows it. How is it used in these compilers? While taking an input string with spaces, the buffer does not get cleared for ...
🌐
Linux Hint
linuxhint.com › fflush-function-c-programming
How to use fflush function in C programming – Linux Hint
The fflush function in C programming is used to clear the buffer memory and display the results of stdout. In this write-up, we have explained the fflush() and with the help of examples. The fflush() function is recommended to use with the stdout because stdout is by default a buffer and saves ...
🌐
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.
🌐
PrepBytes
prepbytes.com › home › c programming › fflush() in c
fflush() in C
August 18, 2023 - It is used to write all pending ... program moves on to other operations. The fflush function in C is used to flush the buffer of any stream present in RAM, which means it prints the data of the buffer to the corresponding file ...
🌐
Cplusplus
cplusplus.com › reference › cstdio › fflush
fflush
In files open for update (i.e., open for both reading and writing), the stream shall be flushed after an output operation before performing an input operation. This can be done either by repositioning (fseek, fsetpos, rewind) or by calling explicitly fflush, like in this example:
Find elsewhere
Top answer
1 of 2
47

The answer to this is that fflush(stream) is only formally defined for output streams, so fflush(stdout) is OK, but fflush(stdin) is not.

The purpose of fflush(stream) is to make the operating system flush any buffers to the underlying file. For an example of a legitimate use, students often have problems like “my prompt doesn't appear!” if they do something like:

printf("Enter a number: ");

However, they find that this works just fine:

printf("Enter a number:\n");

Of course, they don't want a newline after their prompt, so they have a bit of a problem.

The reason for this is that the output to stdout is buffered by the OS and the default behavior is (often) only to actually write the output to the terminal when a newline is encountered. Adding an fflush(stdout) after the printf() solves the problem:

printf("Enter a number: ");
fflush(stdout);

Now, working by analogy, people often think that fflush(stdin) should discard any unused input, but if you think about it a little bit that doesn't make much sense. What does it mean to “flush” an input buffer? Where is it “flushed” to? If you flush an output buffer, the output is sent to the underlying file or the terminal, where it would eventually wind up anyway, but where would input “eventually end up anyway”? There's no way of knowing! What should the behavior be if the input stream data comes from a file or a pipe or a socket? It isn't at all clear for input streams what the behavior of fflush() should be, but it's very clear for output streams in all cases. Hence, fflush() is only defined for output streams.

The reason why the erroneous use of fflush(stdin) became commonplace is that, many years ago, a few operating systems did implement a scheme where it worked as many people expected, discarding unused input. Microsoft DOS is a good example. Surprisingly, modern versions of Linux also implement fflush() for input streams.

The right thing to do with “extra” unwanted terminal input is simply to read it and do nothing with it. This is almost as easy as calling fflush(stdin), works everywhere, and doesn't rely on formally undefined behavior.

The C standard says:

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

POSIX says (also explicitly defers to C standard):

If stream points to an output stream or an update stream in which the most recent operation was not input, fflush() shall cause any unwritten data for that stream to be written to the file, ...

But the Linux manpage says:

For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function. For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application. The open status of the stream is unaffected.

2 of 2
20

fflush(stdin) invokes undefined behaviour.

fflush() is defined only for output streams. You should not do it.


On Unix, Ctrl-Z sends a TSTP signal (SIGTSTP) which by default causes the process to suspend execution.

🌐
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.
🌐
George Washington University
www2.seas.gwu.edu › ~simhaweb › C › modules › appendix › misc › misc.html
Using fflush() to force output
To force output to be written immediately, use fflush(): (source file) 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 ...
🌐
TutorialsPoint
tutorialspoint.com › use-of-fflush-stdin-in-c
Use of fflush(stdin) in C
June 24, 2020 - The function fflush(stdin) is used to flush the output buffer of the stream. It returns zero, if successful otherwise, returns EOF and feof error indicator is set. Here is the syntax of fflush(stdin) in C language,
Top answer
1 of 5
2

The best way to know what it does is the standard

7.21.5.2 The fflush function

Synopsis


  1. #include <stdio.h>
    int fflush(FILE *stream);
    

Description

  1. If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

  2. If stream is a null pointer, the fflush function performs this flushing action on all streams for which the behavior is defined above.

Returns

  1. The fflush function sets the error indicator for the stream and returns EOF if a write error occurs, otherwise it returns zero.

So basically the the library I/O functions use buffers, fflush() moves the data from the buffers to the physical file, that's why fflush(stdin) as you can understand from above is undefined behavior, because fflush() is only defined for output streams.

The above means, that write operations on output streams are not performed immediately, they are buffered, when you call fflush() then they are flushed to the disk, you don't need to fflush() explicitly all the time, for example in

fpritnf(stdout, "Example text\n");

the "\n" at the end of the printed text, triggers a flush automatically, also when you call fclose() or exit the program the buffers are flushed.

You may also notice, that even for output streams the behavior is not defined unless the most recent operation is output, input operations cannot be flushed.

2 of 5
2

Quoting C11 standard, chapter 7.21.5.2

int fflush(FILE *stream);

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush() function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

So, basically, the fflush(stream) call will forcefully (even if the buffer is not full) flush out any data that is present in the buffer associated with that particular stream to the output file (disk).

Also, as a note, from paragraph 3, same document

If stream is a null pointer, the fflush() function performs this flushing action on all streams for which the behavior is defined above.

🌐
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 ...
🌐
Quora
quora.com › What-does-fflush-stdout-do-in-C-and-how-do-you-use-it
What does fflush (stdout) do in C, and how do you use it? - Quora
Answer (1 of 3): Personally, I only find any use in calling fflush(stdout) after making a call to something like “setvbuf(stdout, NULL, _IOFBF, size)”. To understand fflush, you must first understand that when you do write operations (that print to the console, in the case of stdout), what actua...
Top answer
1 of 3
63

It's a little hard to say what "can be problems with" (excessive?) use of fflush. All kinds of things can be, or become, problems, depending on your goals and approaches. Probably a better way to look at this is what the intent of fflush is.

The first thing to consider is that fflush is defined only on output streams. An output stream collects "things to write to a file" into a large(ish) buffer, and then writes that buffer to the file. The point of this collecting-up-and-writing-later is to improve speed/efficiency, in two ways:

  • On modern OSes, there's some penalty for crossing the user/kernel protection boundary (the system has to change some protection information in the CPU, etc). If you make a large number of OS-level write calls, you pay that penalty for each one. If you collect up, say, 8192 or so individual writes into one large buffer and then make one call, you remove most of that overhead.
  • On many modern OSes, each OS write call will try to optimize file performance in some way, e.g., by discovering that you've extended a short file to a longer one, and it would be good to move the disk block from point A on the disk to point B on the disk, so that the longer data can fit contiguously. (On older OSes, this is a separate "defragmentation" step you might run manually. You can think of this as the modern OS doing dynamic, instantaneous defragmentation.) If you were to write, say, 500 bytes, and then another 200, and then 700, and so on, it will do a lot of this work; but if you make one big call with, say, 8192 bytes, the OS can allocate a large block once, and put everything there and not have to re-defragment later.

So, the folks who provide your C library and its stdio stream implementation do whatever is appropriate on your OS to find a "reasonably optimal" block size, and to collect up all output into chunk of that size. (The numbers 4096, 8192, 16384, and 65536 often, today, tend to be good ones, but it really depends on the OS, and sometimes the underlying file system as well. Note that "bigger" is not always "better": streaming data in chunks of four gigabytes at a time will probably perform worse than doing it in chunks of 64 Kbytes, for instance.)

But this creates a problem. Suppose you're writing to a file, such as a log file with date-and-time stamps and messages, and your code is going to keep writing to that file later, but right now, it wants to suspend for a while and let a log-analyzer read the current contents of the log file. One option is to use fclose to close the log file, then fopen to open it again in order to append more data later. It's more efficient, though, to push any pending log messages to the underlying OS file, but keep the file open. That's what fflush does.

Buffering also creates another problem. Suppose your code has some bug, and it sometimes crashes but you're not sure if it's about to crash. And suppose you've written something and it's very important that this data get out to the underlying file system. You can call fflush to push the data through to the OS, before calling your potentially-bad code that might crash. (Sometimes this is good for debugging.)

Or, suppose you're on a Unix-like system, and have a fork system call. This call duplicates the entire user-space (makes a clone of the original process). The stdio buffers are in user space, so the clone has the same buffered-up-but-not-yet-written data that the original process had, at the time of the fork call. Here again, one way to solve the problem is to use fflush to push buffered data out just before doing the fork. If everything is out before the fork, there's nothing to duplicate; the fresh clone won't ever attempt to write the buffered-up data, as it no longer exists.

The more fflush-es you add, the more you're defeating the original idea of collecting up large chunks of data. That is, you are making a tradeoff: large chunks are more efficient, but are causing some other problem, so you make the decision: "be less efficient here, to solve a problem more important than mere efficiency". You call fflush.

Sometimes the problem is simply "debug the software". In that case, instead of repeatedly calling fflush, you can use functions like setbuf and setvbuf to alter the buffering behavior of a stdio stream. This is more convenient (fewer, or even no, code changes required—you can control the set-buffering call with a flag) than adding a lot of fflush calls, so that could be considered a "problem with use (or excessive-use) of fflush".

2 of 3
2

Well, @torek's answer is almost perfect, but there's one point which is not so accurate.

The first thing to consider is that fflush is defined only on output streams.

According to man fflush, fflush can also be used in input streams:

For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function. For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application. The open status of the stream is unaffected. So, when used in input, fflush just discard it.

Here is a demo to illustrate it:

#include<stdio.h>

#define MAXLINE 1024

int main(void) {
  char buf[MAXLINE];

  printf("prompt: ");
  while (fgets(buf, MAXLINE, stdin) != NULL)
    fflush(stdin);
    if (fputs(buf, stdout) == EOF)
      printf("output err");

  exit(0);
}
🌐
TutorialKart
tutorialkart.com › c-programming › c-stdio-fflush
fflush() - C stdio.h - Syntax, Examples
February 23, 2025 - ... #include <stdio.h> int main() { printf("Enter your name: "); fflush(stdout); // Flush output to ensure the prompt is displayed immediately char name[50]; scanf("Is", name); printf("Hello, %s!\n", name); return 0; }
🌐
O'Reilly
oreilly.com › library › view › c-in-a › 0596006977 › re73.html
fflush - C in a Nutshell [Book]
December 16, 2005 - In the following example, the program fflush.c writes two lines of text to a file. If the macro FLUSH is defined, the program flushes the file output buffer to disk after each line. If not, only the first output line is explicitly flushed.
Authors   Peter PrinzTony Crawford
Published   2005
Pages   618
🌐
IBM
ibm.com › docs › en › i › 7.3.0
fflush() — Write Buffer to File
We cannot provide a description for this page right now