Section 7.9.13/7
of c99
states that:
At program start-up, three text streams are predefined and need not be opened explicitly - standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output).
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.
So that makes sense. If you're pushing your standard output to a file, you want it fully buffered for efficiency.
But I can find no mention in the standard as to whether the output is line buffered or unbuffered when you can't determine the device is non-interactive (ie, normal output to a terminal).
The reason I ask was a comment to my answer here that I should insert an fflush(stdout);
between the two statements:
printf ("Enter number> "); // fflush (stdout); needed ? if (fgets (buff, sizeof(buff), stdin) == NULL) { ... }
because I wasn't terminating the printf
with a newline. Can anyone clear this up?
The stream stdout is line-buffered when it points to a terminal. Partial lines will not appear until fflush(3) or exit(3) is called, or a newline is printed. This can produce unexpected results, especially with debugging output.
The main reason why buffering exists is to amortize the cost of these system calls. This is primarily important when the program is doing a lot of these write calls, as the amortization is only effective when the system call overhead is a significant percentage of the program's time.
Line buffering - characters are transmitted to the system as a block when a new-line character is encountered. Line buffering is meaningful only for text streams and UNIX file system files. Full buffering - characters are transmitted to the system as a block when a buffer is filled.
The C99 standard does not specify if the three standard streams are unbuffered or line buffered: It is up to the implementation. All UNIX implementations I know have a line buffered stdin
. On Linux, stdout
in line buffered and stderr
unbuffered.
As far as I know, POSIX does not impose additional restrictions. POSIX's fflush page does note in the EXAMPLES section:
[...] The fflush() function is used because standard output is usually buffered and the prompt may not immediately be printed on the output or terminal.
So the remark that you add fflush(stdout);
is correct.
An alternative could be to make stdout
unbuffered:
setbuf(stdout, NULL); /* or */ setvbuf(stdout, NULL, _IONBF, 0);
But as R. notes you can only do this once, and it must be before you write to stdout
or perform any other operantion on it. (C99 7.19.5.5 2)
I just read a recent thread on comp.lang.c
about the same thing. One of the remarks:
Unix convention is that
stdin
andstdout
are line-buffered when associated with a terminal, and fully-buffered (aka block-buffered) otherwise.stderr
is always unbuffered.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With