2

I'm new to C, sorry if my question is too basic.I often see code like:

printf("%d", counter);
fflush(stdout);

my guess is that it won't print output if the buffer is not full therefore you need to flush the stdout. But I tried to not use fflush, only printf, I still have out put printed on the screen, then what's the point to use flush?

ShadowRanger
  • 108,619
  • 9
  • 124
  • 184
  • 2
    Did the program exit shortly after this code? Open handles are all flushed when closed, including when the program exits, and the output would appear. `fflush` matters when the program isn't going to exit soon, and you might need the output on the screen *now*, e.g. to display a countdown (that you repeatedly erase with `\r` and print over to update), or to ensure data is sent to a pipe that a parallel process is relying on (especially if you're going to be waiting on a response to said data; if you block without flushing, it nevers sees the data, never responds, and you're deadlocked). – ShadowRanger Sep 14 '20 at 01:30
  • I tried not looking both ways before crossing the street and it worked just fine, so why do people look both ways before crossing the street? – David Schwartz Sep 14 '20 at 01:36
  • @dukeforever: What does that have to do with anything? The OP is using `stdout`, which is neither an input, nor an update stream, it's pure output. – ShadowRanger Sep 14 '20 at 01:42
  • 1
    You have a subtle error in your logic slowjams; or, maybe just an error in your wording. You think that the buffer only gets flushed when it is full. That's not completely true. Yes, a full buffer will trigger a flush; but, it's not the only thing that will trigger a flush. Longer times in the buffer, and amount of spare compute / io capacity will also case buffers to flush, even when they aren't full. – Edwin Buck Sep 14 '20 at 01:47
  • @EdwinBuck will new line `\n` also trigger a buffer flush? –  Sep 14 '20 at 01:57
  • 3
    @slowjams There is nothing guaranteed to trigger a buffer flush except `fflush`. Everything else might trigger one; but, there is no guarantee. Some streams flush automatically when written to (in some languages). Typically these are your error streams, not your typical output streams; but, you need to consult the language documentation to really be sure it's guaranteed – Edwin Buck Sep 14 '20 at 02:02
  • Related: [Why does printf not flush after the call unless a newline is in the format string?](https://stackoverflow.com/q/1716296/12149471) – Andreas Wenzel Sep 14 '20 at 02:17

1 Answers1

10

The main reason to use fflush after printf is timing.

printf will display the information, at some point in time. Basically all prints to printf are buffered. fflush guarantees the buffer is emptied, meaning the print happened at the line of code that called fflush.

In programs that tend to crash, fflush can be a very useful tool. Often the message that a user of your program wishes to receive is the last message printed just before the crash. If the program doesn't have a fflush that user will probably not get the last print statement, with the statement being lost in the buffer before display.

This can often lead to developers looking in the wrong place in a program's source code for errors upon analyzing log files after a crash. The mental process is "well, it couldn't have made it here, because there's a printf we would have seen" when the reality is the program passed the "would have been seen" printf statement but died with that message in the buffer. If one flushes statements just after printing them, this doesn't happen (although the program runs slower, because flushing takes time).

Edwin Buck
  • 64,804
  • 7
  • 90
  • 127
  • It may also be worth noting that, by default, `stderr` is not buffered. Therefore, the described problem only exists with `stdout` and not `stderr`. – Andreas Wenzel Sep 14 '20 at 02:25