C
CBFalconer
Mike said:printf("prompt");
getchar();
Data will be retrieved from stdin (assuming any is available).
Dan made no remarks about the output from 'printf()'.![]()
Oh. Apologies for my slowness.
Mike said:printf("prompt");
getchar();
Data will be retrieved from stdin (assuming any is available).
Dan made no remarks about the output from 'printf()'.![]()
In said:Chapter and verse time please.
CBFalconer said:Chapter and verse time please.
The genuine guru I was mentioning above is actually Chris Torek.
- The input and output dynamics of interactive devices shall
take place as specified in 7.19.3. The intent of these
requirements is that unbuffered or line-buffered output appear
as soon as possible, to ensure that prompting messages actually
appear prior to a program waiting for input.
...
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.
Irrwahn said:Just to make sure I get it right: does this mean that the following
program is *guaranteed* to print the prompt before requesting input
(presuming no unexpected I/O errors occur)?
#include <stdio.h>
int main( void )
{
int c;
printf( "Enter a character: " );
c = getchar();
printf( "You entered: %c\n", c );
return 0;
}
In said:Just to make sure I get it right: does this mean that the following
program is *guaranteed* to print the prompt before requesting input
(presuming no unexpected I/O errors occur)?
#include <stdio.h>
int main( void )
{
int c;
printf( "Enter a character: " );
c = getchar();
printf( "You entered: %c\n", c );
return 0;
}
Not by the C standard. The next sentence begins "Support for these
characteristics is implementation-defined".
In said:[output buffer flushed when input is requested, C99 7.19.3#3 ]
Not by the C standard. The next sentence begins "Support for these
characteristics is implementation-defined".
Ack. I should've looked it up before posting...
I will stick to inserting
fflush( stdout );
here.
No *guarantee* at all, but this is the intent of the standard.
For one thing, the implementation is free to decide that your
terminal/console/whatever is not an interactive device. You could
improve your chances, by forcing line buffering on stdout and no
buffering on stdin, though, but the implementation is free to reject
your attempts at doing that...
[output buffer flushed when input is requested, C99 7.19.3#3 ]Irrwahn Grausewitz wrote:
(e-mail address removed) (Dan Pop) wrote:
Just to make sure I get it right: does this mean that the following
program is *guaranteed* to print the prompt before requesting input
(presuming no unexpected I/O errors occur)?
Not by the C standard. The next sentence begins "Support for these
characteristics is implementation-defined".
Ack. I should've looked it up before posting...
I will stick to inserting#include <stdio.h>
int main( void )
{
int c;
printf( "Enter a character: " );
fflush( stdout );
here.
If the implementation is perverse enough, it wouldn't help. Any
reasonable implementation would follow the intent of the standard and
the fflush call is useless.
The genuine guru I was mentioning above is actually Chris Torek.
If the implementation is perverse enough, it wouldn't help. Any
reasonable implementation would follow the intent of the standard and
the fflush call is useless.
Irrwahn Grausewitz wrote:
(e-mail address removed) (Dan Pop) wrote:
[output buffer flushed when input is requested, C99 7.19.3#3 ]
Just to make sure I get it right: does this mean that the following
program is *guaranteed* to print the prompt before requesting input
(presuming no unexpected I/O errors occur)?
Not by the C standard. The next sentence begins "Support for these
characteristics is implementation-defined".
Ack. I should've looked it up before posting...
#include <stdio.h>
int main( void )
{
int c;
printf( "Enter a character: " );
I will stick to inserting
fflush( stdout );
here.
If the implementation is perverse enough, it wouldn't help. Any
reasonable implementation would follow the intent of the standard and
the fflush call is useless.
I agree it's useless in the above code, but if we introduce a bug that produces
a segmentation fault:
#include <stdio.h>
int main( void )
{
int *p = NULL;
int c;
printf( "Enter a character: " );
*p = 1;
c = getchar();
printf( "You entered: %c\n", c );
return 0;
}
then an fflush() after the first printf() will ease debugging. The above will
produce no output so the user won't know where to start looking for the problem,
whereas this:
#include <stdio.h>
int main( void )
{
int *p = NULL;
int c;
printf( "Enter a character: " );
fflush(stdout);
*p = 1;
c = getchar();
printf( "You entered: %c\n", c );
return 0;
}
will produce the "Enter a character" prompt so the user will know to start
looking for the bug after that point.
Jeremy said:CBFalconer said:Chapter and verse time please.
7.19.3
3 [...] 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.
The important part is: "When a stream is line buffered [...]
characters are intended to be transmitted as a block to the host
environment [...] when input is requested on a line buffered stream
that requires the transmission of characters from the host
environment."
Is it likely for one to run across a 'moderately perverse'
implementation where fflush would actually help?
in comp.lang.c i read:
there are sufficiently perverse uses of programs such that it can help,
yes. e.g., where stdout is connected to a non-interactive thing which
never the less copies the output to an/the interactive device -- i.e.,
chris torek's recent response re use of a program with a pipe.
the only real reason not to call fflush(stdout) is performance, and you
aren't likely to be shooting for lots of performance with most sorts of
stuff sent to stdout which is intended to provoke data to arrive via stdin.
(as with most things -- this has any number of fairly obvious exceptions.)
In said:[about output buffer flushed when input is requested, C99 7.19.3#3 ]
[email protected] (Dan Pop) said:If the implementation is perverse enough, it wouldn't help. Any
reasonable implementation would follow the intent of the standard and
the fflush call is useless.
Is it likely for one to run across a 'moderately perverse'
implementation where fflush would actually help?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.