Frederick Gotham said:
Ed Prochak posted:
I think I understand...
The following program might not print anything at all:
#include <stdio.h>
int main(void)
{
printf("Hello");
fflush(stdout);
}
While the following *must* print something:
#include <stdio.h>
int main(void)
{
printf("Hello\n");
}
Correct... ?
Yes (though an output error is always a possibility, the first program
will *probably* print "Hello" without a newline, or possibly with one,
on most implementations, and you should really have a "return 0;"
unless you're assuming a C99 implementation).
What are all the ways of "flushing"? I know of:
(1) Print a '\n'.
(2) fflush()
Printing a '\n' doesn't *necessary* flush the stream; it depends on
how the stream is buffered.
C99 7.19.3p3:
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.
C99 7.19.3p7:
At program startup, 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.