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?
Why does printf not flush after the call unless a newline is in the format string
cflushprintf
Related Topic
- What does the ??!??! operator do in C
- Why does C’s printf format string have both %c and %s
- Why does a function with no parameters (compared to the actual function definition) compile
- C++ – the printf format specifier for bool
- Linux – Why does the C preprocessor interpret the word “linux” as the constant “1”
Best Answer
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):Flush
stdout
whenever you need it to usingfflush
:Disable buffering on stdout by using
setbuf
:Or use the more flexible
setvbuf
: