In a normal C program running on a modern OS, file access is buffered twice (or more when you count buffers like the buffer in your drive). One buffer is implemented in the FILE structure and the other is implemented in the kernel.

Often, the FILE structure buffers the content in a buffer inside of your program. When you write something to a buffered file, the content is keep in the buffer, inside of the running program. It is written to the OS when the buffer is full and, when the buffering mode is line buffered, at the end of a line. This data is written to the OS by a syscall, for example write(). The buffer is there because a syscall requires a context switch from the user program to the kernel, this is relatively expensive (slow), the buffer is here to reduce the number of syscalls. You could also use the syscalls from your program directly without the stdio functions, however, this functions are less portable and more complex to handle. A fflush(stdout) checks if there are any data in the buffer that should be written and if so, the underlying syscall is used to write the data to the OS.

When the syscall returns, the data is in your kernel. But modern operating systems buffer this data as well. This is used to reduce the number of disk writes, reduce latency and other things. This buffer is completely independent of the FILE buffer inside your program.

Note that this does not apply to all systems. For example microcontroller environments may provide some stdio.h functions that write directly to a UART, without any buffer, neither inside FILE nor any (probably non-existent) OS.

To see what fflush() does to a running program, compare this programs:

int main(void)
{
  fputs("s",stdout);
  fputs("e",stderr);
}

and

int main(void)
{
  fputs("s",stdout);
  fflush(stdout);
  fputs("e",stderr);
}

On Linux, stderr is not buffered by default, so fputs("e",stderr); will print the data immediately. On the other hand, fputs("s",stdout); is line buffered by default on Linux so the data is not printed immediately. This causes the first program to output es and not se, but the second one outputs se.

You can change the buffer modes with setvbuf()

Answer from 12431234123412341234123 on Stack Overflow
🌐
Quora
quora.com › What-is-the-difference-between-fflush-stdin-and-fflush-stdout
What is the difference between fflush (stdin) and fflush (stdout)? - Quora
In some environments, fflush(stdin) - which is Undefined behavior - used to “clear” an EOF state so you could continue to read characters from stdin.
Top answer
1 of 2
10

In a normal C program running on a modern OS, file access is buffered twice (or more when you count buffers like the buffer in your drive). One buffer is implemented in the FILE structure and the other is implemented in the kernel.

Often, the FILE structure buffers the content in a buffer inside of your program. When you write something to a buffered file, the content is keep in the buffer, inside of the running program. It is written to the OS when the buffer is full and, when the buffering mode is line buffered, at the end of a line. This data is written to the OS by a syscall, for example write(). The buffer is there because a syscall requires a context switch from the user program to the kernel, this is relatively expensive (slow), the buffer is here to reduce the number of syscalls. You could also use the syscalls from your program directly without the stdio functions, however, this functions are less portable and more complex to handle. A fflush(stdout) checks if there are any data in the buffer that should be written and if so, the underlying syscall is used to write the data to the OS.

When the syscall returns, the data is in your kernel. But modern operating systems buffer this data as well. This is used to reduce the number of disk writes, reduce latency and other things. This buffer is completely independent of the FILE buffer inside your program.

Note that this does not apply to all systems. For example microcontroller environments may provide some stdio.h functions that write directly to a UART, without any buffer, neither inside FILE nor any (probably non-existent) OS.

To see what fflush() does to a running program, compare this programs:

int main(void)
{
  fputs("s",stdout);
  fputs("e",stderr);
}

and

int main(void)
{
  fputs("s",stdout);
  fflush(stdout);
  fputs("e",stderr);
}

On Linux, stderr is not buffered by default, so fputs("e",stderr); will print the data immediately. On the other hand, fputs("s",stdout); is line buffered by default on Linux so the data is not printed immediately. This causes the first program to output es and not se, but the second one outputs se.

You can change the buffer modes with setvbuf()

2 of 2
5

When stdout points to a tty, it is, by default, line-buffered. This means the output is buffered inside the computer internals until a full line is received (and output).

Your programs do not send a full line to the computer internals.

In the case of using fflush() you are telling the computer internals to send the current data in the buffer to the device; without fflush() you are relying on the computer internals to do that for you at program termination.

By computer internals I mean the combination of the C library, Operating System, hardware interface, (automatic) buffers between the various interfaces, ...

Discussions

operating system - What does fflush(stdin) do in C programing? - Stack Overflow
The answer to this is that ... 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: ... 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 ... More on stackoverflow.com
🌐 stackoverflow.com
[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
c - Understanding the need for fflush() and problems associated with it - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
when to fflush stdout?
The C Standard is delightfully vague on when a stream's buffer is automatically flushed, or "transmitted to the host environment" as it calls it. First, you need to consider whether the output stream is unbuffered, fully buffered or line buffered: When a stream is unbuffered, characters written to it are intended to be transmitted as soon as possible. When a stream is fully buffered, characters written to its buffer are intended to be transmitted as a block when that buffer is full. When a stream is line buffered, characters written to its buffer are intended to be transmitted as a block when a newline is written. It also says that the buffer is intended to be transmitted as a block when it is full. This can happen even on a line buffered stream, if you don't write a \n for a long time. There are several weasel words throughout all this, things like "intended to be" and "as soon as possible". Put simply, the C Standard says that what happens is pretty much implementation-defined, but it'd be kind of nice if the implementation worked this way. So, if your stdout is unbuffered, or if your stdout is line buffered and you are on an implementation that reliably performs an automatic flush whenever you write \n to it (to be honest, you probably will be), then you do not need to do anything extra. Note that the initial buffering mode for stdout is not fully specified by the C Standard. It requires stdout to be fully buffered if and only if the implementation can determine it is not connected to an interactive device. This means that if it is connected to an interactive device it could be unbuffered or line buffered. Luckily for you, however, they should have the same behaviour when you write \n. More on reddit.com
🌐 r/C_Programming
6
1
February 3, 2021
🌐
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 ...
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.

🌐
GeeksforGeeks
geeksforgeeks.org › c language › use-fflushstdin-c
Use of fflush(stdin) in C - GeeksforGeeks
September 15, 2023 - 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). Below is its syntax. fflush(FILE *ostream); ostream points to an output stream or an update ...
🌐
Sololearn
sololearn.com › en › Discuss › 117448 › what-is-difference-b-w-fflush-stdin-and-fflush-stdout-in-c-c
What is Difference b/w fflush(stdin) and fflush(stdout) in C/C++? | Sololearn: Learn to code for FREE!
buffer is some amount of memory ... we need to do manually by using fflush fflush is used to clear the data inside this memory so that it can be used to write new data into it ... stdout and stdin are two standard output ...
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 61997-when-where-use-fflush-stdout.html
When and where to use fflush (stdout)?
If the line of text does not end in a \n, there is no guarantee that any of the text will appear on the screen. fflush() moves everything that is "pending" onto the screen. fflush(stdout) means write all of the buffered data to it's destination - whatever stdout is.
Find elsewhere
🌐
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".

🌐
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.
🌐
Alibaba Cloud
topic.alibabacloud.com › a › fflush-stdin-and-fflush-stdout_8_8_20216967.html
Fflush (stdin) and fflush (stdout)
July 26, 2018 - 1.fflush (stdin): Role: Clean up the standard input stream and throw away the excess data that has not been saved. Such as: int main () { int num; Char str[10]; cin>>num; cout>str; cout Get an integer
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);
}
🌐
Cprogramming
faq.cprogramming.com › cgi-bin › smartfaq.cgi
Why fflush(stdin) is wrong - FAQ - Cprogramming.com
How to begin Get the book · C tutorial C++ tutorial Game programming Graphics programming Algorithms More tutorials
Top answer
1 of 6
1
>>When and why we need to use fflush exactly? Depends on the operating system. When writing to the screen (MS-Windows) I never use fflush() because MS-Windows seems to write immediately. I have seen the problem on *nix computers where it was necessary even after putting '\n' in the output stream. · When to do it? You won't have to put it after every printf() statement, you can delay callinf fflush() until after all printf() statements but before any input statements. Lets say you want to display a menu that has 10 printf() statements. Just put the fflush() after the last printf(). · For disk file streams call fflush() after all writing has been done. Its not necessary to call fflush() before the file is closed because fclose() will do that anyway.
2 of 6
1
in the ... link they are saying, user should definitely NOT [use fflush(stdout) to clear the output buffer.] · that webpage is a joke. a bad joke. they actually instruct you to use gets() as an example of "safe code". this right there tells me they're talking out their ass. · for example here jephthah gave a solution where he used fflush after printf and mentioned // printf did not have newline, need to flush · if you don't print a newline, but want the line to display to the terminal, you should use fflush(stdout);. Your system may not need this, but some systems will not print text to the output buffer until a newline is found or the buffer is otherwise flushed. · Narue has explained it better than i can. i'm only commenting here because my name was dropped :icon_wink:
🌐
ZetCode
zetcode.com › clang › fflush
C fflush Tutorial: Mastering Output Buffering with Practical Examples
Here, fflush(stdout) ensures prompts appear before waiting for input. Note that fflush(stdin) is undefined behavior in standard C. To clear input buffers, read characters until \n or EOF as shown. This approach works portably across different platforms.
🌐
Linux Man Pages
man7.org › linux › man-pages › man3 › fflush.3.html
fflush(3) - Linux manual page
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.
🌐
Reddit
reddit.com › r/c_programming › when to fflush stdout?
r/C_Programming on Reddit: when to fflush stdout?
February 3, 2021 -

I'M under openwrt.

call vfprintf(stdout, buf, ap) or vprintf
buf endwith "\n"
do i need to fflush it?

Top answer
1 of 2
3
The C Standard is delightfully vague on when a stream's buffer is automatically flushed, or "transmitted to the host environment" as it calls it. First, you need to consider whether the output stream is unbuffered, fully buffered or line buffered: When a stream is unbuffered, characters written to it are intended to be transmitted as soon as possible. When a stream is fully buffered, characters written to its buffer are intended to be transmitted as a block when that buffer is full. When a stream is line buffered, characters written to its buffer are intended to be transmitted as a block when a newline is written. It also says that the buffer is intended to be transmitted as a block when it is full. This can happen even on a line buffered stream, if you don't write a \n for a long time. There are several weasel words throughout all this, things like "intended to be" and "as soon as possible". Put simply, the C Standard says that what happens is pretty much implementation-defined, but it'd be kind of nice if the implementation worked this way. So, if your stdout is unbuffered, or if your stdout is line buffered and you are on an implementation that reliably performs an automatic flush whenever you write \n to it (to be honest, you probably will be), then you do not need to do anything extra. Note that the initial buffering mode for stdout is not fully specified by the C Standard. It requires stdout to be fully buffered if and only if the implementation can determine it is not connected to an interactive device. This means that if it is connected to an interactive device it could be unbuffered or line buffered. Luckily for you, however, they should have the same behaviour when you write \n.
2 of 2
1
Related thing, concerning line buffered FILEs: printf("yes "); /* no newline */ getchar(); The prompt gets out without a fflush, at least here, because input from a line/non-buffered stream forces fflush on ALL line buffered output streams automatically. ?! There's a comment in FreeBSD stdio sources: /* * Before reading from a line buffered or unbuffered file, * flush all line buffered output files, per the ANSI C * standard. */
🌐
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,
🌐
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. This is particularly useful in displaying output, as the operating system may initially put the output data in a temporary buffer before writing it to ...
🌐
Programiz
programiz.com › cpp-programming › library-function › cstdio › fflush
C++ fflush() - C++ Standard Library
If stream is an output stream or update stream whose last operation was output, calling the fflush() function will write any buffered unwritten data to the associated output device. If stream is a null pointer, all open output streams are flushed. The behaviour is undefined for input streams ...