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
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, ...

🌐
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).
Discussions

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
Fflush(stdout) doesn't work as expected.
You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. ... Thank you for taking the time to submit an issue! ... It appears to me that fflush(stdout) has no effect, where previously it would cause the ... More on github.com
🌐 github.com
10
June 1, 2024
fflush does not work on stdout
My C program is: #include int main() { printf("Hey1\nHey"); fflush(stdout); return 0; } And when executed in a real unix environment is outputs both Hey1 and Hey, but on t... More on github.com
🌐 github.com
3
October 23, 2018
c - Write to file not visible before close; fflush(stdout) ineffective - Stack Overflow
Running the code gives me an output ... line, it does print correctly. I've searched about this already, but what I've found is that it needs to be flushed, but I do this (though apparently incorrectly). What am I doing wrong? ... You're flushing stdout.... More on stackoverflow.com
🌐 stackoverflow.com
🌐
George Washington University
www2.seas.gwu.edu › ~simhaweb › C › modules › appendix › misc › misc.html
Using fflush() to force output
fflush() forces the output to be written from whatever buffer is being used by its argument. The argument is a file. In C, the screen is treated like a file - it has the special name stdout.
🌐
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 › 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.
🌐
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...
🌐
CodeGuru Forums
forums.codeguru.com › showthread.php
fflush(stdout)
If I want to write code for C89 It's necessary to use fflush(stdout) during message print to output ? E.g. with printf("message") .
Find elsewhere
🌐
GitHub
github.com › open-mpi › ompi › issues › 12594
Fflush(stdout) doesn't work as expected. · Issue #12594 · open-mpi/ompi
June 1, 2024 - You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. ... Thank you for taking the time to submit an issue! ... It appears to me that fflush(stdout) has no effect, where previously it would cause the stdout buffer to flush as one might expect.
Author   WSaffery
🌐
The Open Group
pubs.opengroup.org › onlinepubs › 000095399 › functions › fflush.html
fflush
The following example uses printf() calls to print a series of prompts for information the user must enter from standard input. The fflush() calls force the output to standard output. The fflush() function is used because standard output is usually buffered and the prompt may not immediately ...
🌐
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 ...
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:
🌐
Arch Linux Forums
bbs.archlinux.org › viewtopic.php
fflush(stdout) not flushing all output - SOLVED / Programming & Scripting / Arch Linux Forums
May 12, 2008 - Most of the time the filtering is just converting the 0x02 into "<STX>" and the 0x03 into "<ETX>\n", the rest of the data is ascii and just dumps to stdout.
🌐
cppreference.com
en.cppreference.com › w › c › io › fflush.html
fflush - cppreference.com
If stream is a null pointer, all open output streams are flushed, including the ones manipulated within library packages or otherwise not directly accessible to the program.
🌐
Google Translate
translate.google.com › translate
Why do we use the function fflush (stdout) in C? - Quora
Answer (1 of 4): In most implementations (certainly Linux, MacOS and Windows) the stdin/stdout streams are buffered. So if you printf (“Hello World\n”); - there is no guarantee when that message will actually be sent to the screen (or to disk if you printf to a file) at any time in the near futu...
🌐
GitHub
github.com › emscripten-core › emscripten › issues › 7360
fflush does not work on stdout · Issue #7360 · emscripten-core/emscripten
October 23, 2018 - My C program is: #include <stdio.h> int main() { printf("Hey1\nHey"); fflush(stdout); return 0; } And when executed in a real unix environment is outputs both Hey1 and Hey, but on the browser only Hey1 is printed. When I call stream.tty.ops.flush(stream.tty) in the browser console, the Hey is printed.
Author   TonyLianLong
🌐
KooR
koor.fr › C › cstdio › fflush.wp
KooR.fr - fflush - Langage C
La fonction fflush permet de forcer explicitement cette synchronisation. stream: ce paramètre permet d'indiquer le flux de caractères pour lequel le buffer en mémoire associé doit être synchronisé.
🌐
IBM
ibm.com › docs › ar › i › 7.4.0
fflush() — Write Buffer to File
We cannot provide a description for this page right now
🌐
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 ...