F
Fernando Arbeiza
Hi:
I need some clarification about a code like this:
printf("%s", "a string with NO trailing newline");
scanf("%d", &i);
Regarding if a fflush() of the standard output is needed or not. I think
the relevant portions of the standard are:
[7.19.3.3] When a stream is unbuffered, characters are intended to
appear from the source or at the destination as soon as possible. [...]
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.
[7.19.3.7] [...] 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.
We have been discussing about it, and there are two opinions:
- The fflush(stdout) after printf() is not necessary, since the
standard guarantees that the string is transmitted to the
environment when the input is requested.
- The fflush(stdout) after printf() is desirable, since the
implicit fflush() is not guaranteed when input is requested. It is
not guaranteed because the standard specify that "support for
these characteristics is implementation-defined".
Could you, please, tell me which (if any) is the correct reasoning?
Perhaps it is a matter of language. English is not my mother tongue, so
I may have misinterpreted the standard.
Thanks in advance,
I need some clarification about a code like this:
printf("%s", "a string with NO trailing newline");
scanf("%d", &i);
Regarding if a fflush() of the standard output is needed or not. I think
the relevant portions of the standard are:
[7.19.3.3] When a stream is unbuffered, characters are intended to
appear from the source or at the destination as soon as possible. [...]
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.
[7.19.3.7] [...] 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.
We have been discussing about it, and there are two opinions:
- The fflush(stdout) after printf() is not necessary, since the
standard guarantees that the string is transmitted to the
environment when the input is requested.
- The fflush(stdout) after printf() is desirable, since the
implicit fflush() is not guaranteed when input is requested. It is
not guaranteed because the standard specify that "support for
these characteristics is implementation-defined".
Could you, please, tell me which (if any) is the correct reasoning?
Perhaps it is a matter of language. English is not my mother tongue, so
I may have misinterpreted the standard.
Thanks in advance,