582

Why does printf not flush after the call unless a newline is in the format string? Is this POSIX behavior? How might I have printf immediately flush every time?

K DawG
  • 11,686
  • 9
  • 28
  • 63
Crazy Chenz
  • 10,877
  • 12
  • 44
  • 57
  • 2
    did you investigated whether this happens with any file or only with terminals? that would sound to be a clever terminal feature not to output uncompleted line from a background program, though i expect it wouldn't apply to *the* foreground program. – PypeBros Nov 12 '09 at 16:50
  • 8
    Under Cygwin bash I'm seeing this same misbehaviour even if a newline _is_ in the format string. This problem is new to Windows 7; the same source code worked fine on Windows XP. MS cmd.exe flushes as expected. The fix `setvbuf(stdout, (char*)NULL, _IONBF, 0)` works around the problem, but surely should not have been necessary. I'm using MSVC++ 2008 Express. ~~~ – Steve Pitchers Jan 08 '13 at 14:10
  • 10
    To clarify the title of the question: `printf(..)` **does not do any flushing** itself, it's the buffering of `stdout` that may flush when seeing a newline (if it's line-buffered). It would react the same way to `putchar('\n');`, so `printf(..)` is not special in this regard. This is in contrast with `cout << endl;`, the [documentation of which](http://www.cplusplus.com/reference/ostream/endl/) prominently mentions flushing. The [documentation of printf](http://www.cplusplus.com/reference/cstdio/printf/) doesn't mention flushing at all. – Evgeni Sergeev Apr 05 '16 at 14:02
  • 1
    writing (/flushing) is potentially an expensive operation, it's probably buffered for performance reasons. – hanshenrik Aug 17 '17 at 23:31
  • @EvgeniSergeev: Is there a consensus that the question has incorrectly diagnosed the problem, and that flushing happens when there is a newline in the *output*? (putting one in the format string is one way, but not the only way, of getting one in the output). – Ben Voigt May 01 '20 at 17:54

10 Answers10

767

The stdout stream is line buffered by default, so will only display what's in the buffer after it reaches a newline (or when it's told to). You have a few options to print immediately:

Print to stderrinstead using fprintf (stderr is unbuffered by default):

fprintf(stderr, "I will be printed immediately");

Flush stdout whenever you need it to using fflush:

printf("Buffered, will be flushed");
fflush(stdout); // Will now print everything in the stdout buffer

Edit: From Andy Ross's comment below, you can also disable buffering on stdout by using setbuf:

setbuf(stdout, NULL);

or its secure version setvbuf as explained here

setvbuf(stdout, NULL, _IONBF, 0); 
Rudd Zwolinski
  • 23,948
  • 17
  • 54
  • 60
  • 273
    Or, to disable buffering entirely: `setbuf(stdout, NULL);` – Andy Ross Nov 11 '09 at 17:42
  • 87
    Also, just wanted to mention that apparently in UNIX a newline will typically only flush the buffer if stdout is a terminal. If the output is being redirected to a file, a newline won't flush. – hora Mar 05 '11 at 23:10
  • 6
    I feel that I should add: I've just been testing this theory, and I am finding that using `setlinebuf()` on a stream which is not directed to a terminal *is* flushing at the end of each line. – Doddy Sep 06 '11 at 19:06
  • @bean Well of course it does, that is the whole point of it and it doesn't require a newline with setlinebuf depending on the argument you provide. You can do one of the following: unbuffered, block buffered, and line buffered. Unbuffered will not require a newline and information will appear on the destination file or terminal as soon as it is available. – xshoppyx Apr 02 '13 at 07:56
  • 8
    "As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device" -- see this question: http://stackoverflow.com/questions/5229096/does-printf-always-flush-the-buffer-on-encountering-a-newline – Seppo Enarvi May 22 '15 at 07:23
  • Is there a way to do this persistently in like an IDE option? – Hack-R Jun 01 '15 at 17:38
  • Just tested, on Mac OS X 10.10.5 (Yosemite) setbuf(stdout, NULL); does NOT have the desired effect. – nyholku Nov 26 '15 at 14:25
  • 3
    @RuddZwolinski If this is going to be a good canon answer of "why isn't it printing" it seems important to mention the terminal/file distinction as per ["Does printf always flush the buffer on encountering a newline?"](http://stackoverflow.com/questions/5229096/does-printf-always-flush-the-buffer-on-encountering-a-newline) directly in this highly upvoted answer, vs people needing to read the comments... – HostileFork says dont trust SE Apr 08 '16 at 22:08
  • Also it should explain, why streams are buffered at all in in the first place. That is important to know in order to decide whether to "unbuffer". – con-f-use Apr 29 '16 at 13:44
  • the standard says that stdout is initially fully buffered ***if*** the ***output device*** can be determined to be a ***non-interactive*** one. http://stackoverflow.com/questions/5229096/does-printf-always-flush-the-buffer-on-encountering-a-newline – Gab是好人 Apr 24 '17 at 20:36
  • Worth mentioning also `exit` function from the standard library will flush the buffer. – Kamil.S Sep 23 '17 at 20:03
  • Do I need `fflush(stdout);` if I am using `fprintf()` to print on a file? – Sigur Nov 20 '17 at 15:06
  • "so will only display what's in the buffer after it reaches a newline (or when it's told to)" --> other cases exists: when the buffer is full. – chux - Reinstate Monica Dec 22 '17 at 10:19
  • link to `setvbuf` is a dead link – pb2q Oct 15 '20 at 14:26
  • "The stdout stream is line buffered by default" C has "As initially opened, ... standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.". As I read, the _default_ for `stdout` with an interactive device is unbuffered or line buffered. Certainly line buffered is more common default, but not a C specified default. – chux - Reinstate Monica Dec 20 '20 at 23:11
  • How is `setvbuf()` more secure than `setbuf()`? The calls to those two functions you give are [strictly equivalent](//en.cppreference.com/w/c/io/setbuf). – Deduplicator May 11 '21 at 22:46
134

No, it's not POSIX behaviour, it's ISO behaviour (well, it is POSIX behaviour but only insofar as they conform to ISO).

Standard output is line buffered if it can be detected to refer to an interactive device, otherwise it's fully buffered. So there are situations where printf won't flush, even if it gets a newline to send out, such as:

myprog >myfile.txt

This makes sense for efficiency since, if you're interacting with a user, they probably want to see every line. If you're sending the output to a file, it's most likely that there's not a user at the other end (though not impossible, they could be tailing the file). Now you could argue that the user wants to see every character but there are two problems with that.

The first is that it's not very efficient. The second is that the original ANSI C mandate was to primarily codify existing behaviour, rather than invent new behaviour, and those design decisions were made long before ANSI started the process. Even ISO nowadays treads very carefully when changing existing rules in the standards.

As to how to deal with that, if you fflush (stdout) after every output call that you want to see immediately, that will solve the problem.

Alternatively, you can use setvbuf before operating on stdout, to set it to unbuffered and you won't have to worry about adding all those fflush lines to your code:

setvbuf (stdout, NULL, _IONBF, BUFSIZ);

Just keep in mind that may affect performance quite a bit if you are sending the output to a file. Also keep in mind that support for this is implementation-defined, not guaranteed by the standard.

ISO C99 section 7.19.3/3 is the relevant bit:

When a stream is unbuffered, characters are intended to appear from the source or at the destination as soon as possible. Otherwise characters may be accumulated and transmitted to or from the host environment as a block.

When a stream is fully buffered, characters are intended to be transmitted to or from the host environment as a block when a buffer is filled.

When a stream is line buffered, characters are intended to be transmitted to or from the host environment as a block when a new-line character is encountered.

Furthermore, characters are intended to be transmitted as a block to the host environment when a buffer is filled, when input is requested on an unbuffered stream, or when input is requested on a line buffered stream that requires the transmission of characters from the host environment.

Support for these characteristics is implementation-defined, and may be affected via the setbuf and setvbuf functions.

Community
  • 1
  • 1
paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
  • 9
    I just came across a scenario where even there is a '\n', printf() doesn't flush. It was overcome by adding a fflush(stdout), as you mentioned here. But I am wondering the reason why '\n' failed to flush the buffer in printf(). – Qiang Xu Apr 28 '12 at 19:45
  • 11
    @QiangXu, standard output is line buffered only in the case where it can be definitively determined to refer to an interactive device. So, for example, if you redirect output with `myprog >/tmp/tmpfile`, that is fully buffered rather than line buffered. From memory, the determination as to whether your standard output is interactive is left to the implementation. – paxdiablo Apr 29 '12 at 00:20
  • 3
    furthermore on Windows calling setvbuf(...., _IOLBF) will not work as _IOLBF is the same as _IOFBF there: https://msdn.microsoft.com/en-us/library/86cebhfs.aspx – Piotr Lopusiewicz Feb 05 '15 at 10:02
29

It's probably like that because of efficiency and because if you have multiple programs writing to a single TTY, this way you don't get characters on a line interlaced. So if program A and B are outputting, you'll usually get:

program A output
program B output
program B output
program A output
program B output

This stinks, but it's better than

proprogrgraam m AB  ououtputputt
prproogrgram amB A  ououtputtput
program B output

Note that it isn't even guaranteed to flush on a newline, so you should flush explicitly if flushing matters to you.

Southern Hospitality
  • 1,150
  • 1
  • 7
  • 12
28

To immediately flush call fflush(stdout) or fflush(NULL) (NULL means flush everything).

Cristian Ciupitu
  • 18,164
  • 7
  • 46
  • 70
Aaron
  • 8,679
  • 4
  • 38
  • 38
  • 35
    Keep in mind `fflush(NULL);` is usually a very bad idea. It will kill performance if you have many files open, especially in a multi-threaded environment where you'll fight with everything for locks. – R.. GitHub STOP HELPING ICE Jun 09 '11 at 13:57
15

Note: Microsoft runtime libraries do not support line buffering, so printf("will print immediately to terminal"):

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setvbuf

phuclv
  • 27,258
  • 11
  • 104
  • 360
Renato
  • 151
  • 1
  • 2
  • 3
    Worse than `printf` going immediately to the terminal in the "normal" case is the fact that `printf` and `fprintf` get more coarsely buffered even in cases where their output is put to immediate use. Unless MS has fixed things, that makes it impossible for one program to capture stderr and stdout from another and identify in what sequence things were sent to each. – supercat Aug 13 '15 at 18:37
  • no, it doesn't print that immediately to the terminal unless no buffering was set. By default full buffering is used – phuclv Apr 26 '20 at 02:01
13

stdout is buffered, so will only output after a newline is printed.

To get immediate output, either:

  1. Print to stderr.
  2. Make stdout unbuffered.
Douglas Leeder
  • 49,001
  • 8
  • 86
  • 133
11

by default, stdout is line buffered, stderr is none buffered and file is completely buffered.

woso
  • 271
  • 4
  • 6
10

You can fprintf to stderr, which is unbuffered, instead. Or you can flush stdout when you want to. Or you can set stdout to unbuffered.

Rasmus Kaj
  • 3,709
  • 18
  • 21
10

Use setbuf(stdout, NULL); to disable buffering.

3

There are generally 2 levels of buffering-

1. Kernel buffer Cache (makes read/write faster)

2. Buffering in I/O library (reduces no. of system calls)

Let's take example of fprintf and write().

When you call fprintf(), it doesn't wirte directly to the file. It first goes to stdio buffer in the program's memory. From there it is written to the kernel buffer cache by using write system call. So one way to skip I/O buffer is directly using write(). Other ways are by using setbuff(stream,NULL). This sets the buffering mode to no buffering and data is directly written to kernel buffer. To forcefully make the data to be shifted to kernel buffer, we can use "\n", which in case of default buffering mode of 'line buffering', will flush I/O buffer. Or we can use fflush(FILE *stream).

Now we are in kernel buffer. Kernel(/OS) wants to minimise disk access time and hence it reads/writes only blocks of disk. So when a read() is issued, which is a system call and can be invoked directly or through fscanf(), kernel reads the disk block from disk and stores it in a buffer. After that data is copied from here to user space.

Similarly that fprintf() data recieved from I/O buffer is written to the disk by the kernel. This makes read() write() faster.

Now to force the kernel to initiate a write(), after which data transfer is controlled by hardware controllers, there are also some ways. We can use O_SYNC or similar flags during write calls. Or we could use other functions like fsync(),fdatasync(),sync() to make the kernel initiate writes as soon as data is available in the kernel buffer.

o_O
  • 55
  • 8