Strange C Problem

Heather Burris heather at maui.cs.ucla.edu
Thu May 11 00:09:33 AEST 1989


In article <13812 at paris.ics.uci.edu> bvickers at bonnie.ics.uci.edu (Brett J. Vickers) writes:
><stuff deleted>
>
>void output_msgs()
>{
>  <stuff>
>  while (current != NULL) {
>    printf("%s\n",current->string);
>    printf("&");
>    <other stuff>
>  }
>  <other stuff>
>}
>
>After the function has output its last message, it fails to continue
>on to the next line (printf("&")).  The ampersand isn't output until
>the next call to output_msgs().  
>Why is this happening?   All help appreciated.

The printf() function prints to stdout and the characteristics
of the what's get printed depends on the what stdout points to. Assuming
that the following program was run on UNIX and that stdout was directed
to the terminal screen, the output will be line-buffered by default; 
nothing will print out until a newline occurs in the output (some 
implementations also flush output if a read from stdin is done).  If 
stdout is redirected to a file, the output will be block buffered.
The manual page setbuf() explains the normal conventions.

If you want the output to print out even though you are not printing
a newline, use fflush(stdout).

Non-UNIX may or may not emulate the UNIX behavior (they may not be
able to if they are unable to determine whether stdout points to
a file or the terminal).

Heather Burris, UCLA



More information about the Comp.lang.c mailing list