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?
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
stderr
instead usingfprintf
(stderr
is unbuffered by default):fprintf(stderr, "I will be printed immediately");
Flush
stdout
whenever you need it to usingfflush
:printf("Buffered, will be flushed"); fflush(stdout); // Will now print everything in the stdout buffer
Disable buffering on stdout by using
setbuf
:setbuf(stdout, NULL);
Or use the more flexible
setvbuf
:setvbuf(stdout, NULL, _IONBF, 0);
setbuf(stdout, NULL);
–
Smoulder setlinebuf()
on a stream which is not directed to a terminal is flushing at the end of each line. –
Bulbul exit
function from the standard library will flush the buffer. –
Unison fflush(stdout);
if I am using fprintf()
to print on a file? –
Appellation stdout
with an interactive device is unbuffered or line buffered. Certainly line buffered is more common default, but not a C specified default. –
Recipe setvbuf()
more secure than setbuf()
? The calls to those two functions you give are strictly equivalent. –
Shinto No, it's not POSIX behaviour, it's ISO C behaviour (well, it is POSIX behaviour but only insofar as they conform to ISO C).
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 89 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 C 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
andsetvbuf
functions.
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. –
Chesser 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.
usually
in this answer - there are plenty of scenarios (colors, long lines, stderr) where the output will be interlaced. –
Fungi To immediately flush call fflush(stdout)
or fflush(NULL)
(NULL
means flush everything).
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. –
Diffusive stdout is buffered, so will only output after a newline is printed.
To get immediate output, either:
- Print to stderr.
- Make stdout unbuffered.
fflush(stdout)
. –
Platte stderr
(this answer mentions later), fflush(stdout)
, fflush(NULL)
. –
Recipe Note: Microsoft runtime libraries do not support line buffering, so printf("will print immediately to terminal")
:
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setvbuf
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. –
Mauri by default, stdout is line buffered, stderr is none buffered and file is completely buffered.
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.
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.
© 2022 - 2024 — McMap. All rights reserved.
setvbuf(stdout, (char*)NULL, _IONBF, 0)
works around the problem, but surely should not have been necessary. I'm using MSVC++ 2008 Express. ~~~ – Shugartprintf(..)
does not do any flushing itself, it's the buffering ofstdout
that may flush when seeing a newline (if it's line-buffered). It would react the same way toputchar('\n');
, soprintf(..)
is not special in this regard. This is in contrast withcout << endl;
, the documentation of which prominently mentions flushing. The documentation of printf doesn't mention flushing at all. – Titania