By Crazy Chenz


2009-11-11 16:22:39 8 Comments

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?

9 comments

@Aaron 2009-11-11 16:26:05

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

@R.. 2011-06-09 13:57:51

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.

@dnahc araknayirp 2015-05-31 03:22:52

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

@Renato 2010-10-26 20:47:31

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

http://msdn.microsoft.com/en-us/library/86cebhfs.aspx

@supercat 2015-08-13 18:37:12

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.

@paxdiablo 2010-11-17 03:52:28

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.

@Qiang Xu 2012-04-28 19:45:39

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

@paxdiablo 2012-04-29 00:20:58

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

@Piotr Lopusiewicz 2015-02-05 10:02:12

furthermore on Windows calling setvbuf(...., _IOLBF) will not work as _IOLBF is the same as _IOFBF there: msdn.microsoft.com/en-us/library/86cebhfs.aspx

@woso 2010-07-29 02:02:19

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

@Rudd Zwolinski 2009-11-11 17:04:46

The stdout stream is buffered, 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 stderr instead using fprintf:

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

@Andy Ross 2009-11-11 17:42:08

Or, to disable buffering entirely: setbuf(stdout, NULL);

@hora 2011-03-05 23:10:56

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.

@Doddy 2011-09-06 19:06:40

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.

@xshoppyx 2013-04-02 07:56:24

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

@Seppo Enarvi 2015-05-22 07:23:05

"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: stackoverflow.com/questions/5229096/…

@Hack-R 2015-06-01 17:38:24

Is there a way to do this persistently in like an IDE option?

@nyholku 2015-11-26 14:25:35

Just tested, on Mac OS X 10.10.5 (Yosemite) setbuf(stdout, NULL); does NOT have the desired effect.

@HostileFork 2016-04-08 22:08:13

@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?" directly in this highly upvoted answer, vs people needing to read the comments...

@con-f-use 2016-04-29 13:44:21

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

@Gab是好人 2017-04-24 20:36:32

the standard says that stdout is initially fully buffered if the output device can be determined to be a non-interactive one. stackoverflow.com/questions/5229096/…

@Kamil.S 2017-09-23 20:03:43

Worth mentioning also exit function from the standard library will flush the buffer.

@Sigur 2017-11-20 15:06:03

Do I need fflush(stdout); if I am using fprintf() to print on a file?

@chux 2017-12-22 10:19:17

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

@Southern Hospitality 2009-11-11 17:54:06

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.

@Rasmus Kaj 2009-11-11 16:26:49

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.

@Douglas Leeder 2009-11-11 16:25:08

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.

@RastaJedi 2016-02-22 22:47:24

Or fflush(stdout).

@chux 2017-12-22 10:20:43

"so will only output after a newline is printed." Not only this but at least 4 other cases. buffer full, write to stderr (this answer mentions later), fflush(stdout), fflush(NULL).

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] Improve INSERT-per-second performance of SQLite?

5 Answered Questions

[SOLVED] How often does python flush to a file?

  • 2010-07-02 16:30:41
  • Tim McJilton
  • 181415 View
  • 193 Score
  • 5 Answer
  • Tags:   python file flush

2 Answered Questions

[SOLVED] "printf" doesn't print a string immediatly

  • 2012-08-27 20:35:21
  • Andrea Sylar Solla
  • 5783 View
  • 2 Score
  • 2 Answer
  • Tags:   c printf

7 Answered Questions

[SOLVED] Printf example in bash does not create a newline

  • 2010-04-20 15:43:25
  • WolfHumble
  • 51252 View
  • 22 Score
  • 7 Answer
  • Tags:   bash printf

3 Answered Questions

[SOLVED] Does printf always flush the buffer on encountering a newline?

  • 2011-03-08 06:23:37
  • Varun Madiath
  • 9245 View
  • 10 Score
  • 3 Answer
  • Tags:   c posix

4 Answered Questions

[SOLVED] Issue in C language using 'fgets' after 'printf' as 'fgets' runs before 'printf'

  • 2012-07-20 07:52:37
  • Tanveer Singh
  • 6177 View
  • 4 Score
  • 4 Answer
  • Tags:   c printf fgets

3 Answered Questions

[SOLVED] Why does stdout need explicit flushing when redirected to file?

  • 2012-12-18 12:23:31
  • Patrick
  • 13735 View
  • 26 Score
  • 3 Answer
  • Tags:   c linux stdout

11 Answered Questions

[SOLVED] Why does C's printf format string have both %c and %s?

  • 2012-06-01 07:16:54
  • Billy Rubina
  • 21019 View
  • 70 Score
  • 11 Answer
  • Tags:   c string-formatting

1 Answered Questions

[SOLVED] Is adding the newline character to printf() equivalent to flushing the stream?

  • 2014-03-13 16:58:49
  • dotslash
  • 426 View
  • 3 Score
  • 1 Answer
  • Tags:   c printf

1 Answered Questions

[SOLVED] printf without \n does not display text when placed before while(1)

  • 2011-09-12 06:35:53
  • Aaveg Mittal
  • 4479 View
  • 3 Score
  • 1 Answer
  • Tags:   c printf

Sponsored Content