From: rad@think.com (Bob Doolittle) Subject: Re: Problem with gcc 2.1 (why do I have to flush stdout?) Date: 28 May 1992 14:22:18
In article <1992May27.051135.23011@cheshire.oxy.edu> rafetmad@cheshire.oxy.edu (David Giller) writes:
rahardj@ccu.umanitoba.ca (Budi Rahardjo) wrote:
>I have problem with gcc 2.1 with a simple (test) program :
>
>#include <stdio.h>
>char line[255];
>main(argc, argv)
>int argc; char *argv[];
>{
> printf("Testing gcc 2.1\n");
> printf("Enter a string: ");
> scanf("%s", line);
> printf("You entered: %s\n", line);
> printf("Enter another string: "); /* I have to flush stdout */
> scanf("%s", line);
> printf("You entered %s\n", line);
>}
>
>When I try to run it, it won't display the "Enter another string:".
>I have to flush stdout explisitly to display it. What gives ?
>Is this a gcc bug or somehere something is buffering my stdout...
Welcome to ANSI C. ANSI C says that you can't rely on printf() flushing
your output. Lots of people are getting bitten my this with the new
IBM compiler for OS/2 also.
I guess you have to add fflush() calls here and there. Seems awfully
kludgy to me, but who ever said the standard way was the best way?
Doesn't setbuf exist under ANSI? It can be used to change the default line
buffering mode for a stream. You can change it to always flush output and
then you don't need fflush(stdout) any more.
-Bob