what is the different of using fflush(stdout) and not using it
Asked Answered
M

2

5
#include <stdio.h>

int main()
{
   printf("Hello");
   fflush(stdout);
   return 0;
}
#include <stdio.h>

int main()
{
   printf("Hello");
   return 0;
}

I'm trying to understand the use of fflush(stdout) and what is the difference between the 2 programs above?

Miso answered 11/8, 2021 at 12:6 Comment(5)
stdout will be flushed upon program termination, so, in this case you'll not notice any difference between the two programs. fflush(stdout) is redundant here.Kalasky
But if you add a noticeable delay between the printf and the fflush then you'll see a difference.System
@JohnBollinger I'd say "if you place a noticeable delay before return 0; in both programs..."Kalasky
That would work too, @TedLyngmo.System
Does this answer your question? Flushing buffers in CLee
J
7

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()

Julietajulietta answered 11/8, 2021 at 12:55 Comment(0)
S
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, ...

Sabin answered 11/8, 2021 at 12:12 Comment(7)
No. NO. NO!!! Output to a tty is line-buffered by default. Output to a regular file is block buffered by default. Don't conflate stdout with a tty.Sacrosanct
What is the difference stdout and tty ? Aren't they the same thing ?Miso
@Kain: $ executable uses tty for stdout ... $ executable >file.txt uses file for stdoutSabin
It is not really the operating system, it is the c library. There is probably also a OS-Level buffer which has nothing to do with the buffer from a FILE *.Julietajulietta
Yes, that's what I meant @12431234123412341234123. Changed OS to computer internalsSabin
@Sabin After fflush(), all the write data hold by the C-Library was written to the OS. So you description of computer internals in combination with the text above is misleading. It is the purpose of fflush() that the data in the C-Library is given to the underlying system (most likely the OS).Julietajulietta
@Miso No. stdout and tty are not the same thing. It is a common mistake to think they are, and you must remove that fundamental misconception from your inner psyche. If you run a command from a shell, the stdout of the shell is inherited by the command. If the shell's stdout happens to be associated with a tty (as is often the case with interactive shells), then the stdout of the process will go to the tty. But they are not the same thing.Sacrosanct

© 2022 - 2024 — McMap. All rights reserved.