Strange C Problem
Gordon C. Galligher
gorpong at telxon.UUCP
Thu May 11 03:55:30 AEST 1989
In article <13812 at paris.ics.uci.edu>, bvickers at bonnie.ics.uci.edu
(Brett J. Vickers) writes:
>
[..much of example deleted..]
> while (current != NULL) {
> printf("%s\n",current->string);
> printf("&");
[..rest of example deleted..]
>
> ...strange happens.
> 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().
The subroutine printf() normally does its output to standard output, which on
most systems is considered a line-buffered output device. This causes all
of the output sent to stdout to wait until it either gets a carriage return,
or if there is a request for input (the output is flushed before it waits for
input). You can force the printing of the trailing ampersand in a few different
ways:
1). Change the printf from printf("&") to printf("&\n");
(Note this will cause an extra line feed each iteration through
the while loop, if you do not want this, then use 2 or 3).
* 2). Include fflush(stdout); either (i) directly after the printf()
(which causes it to be a wasted when more are pending) or
(ii) put the fflush(stdout) after the WHILE loop exits and
before output_msg() returns.)
(fflush(FILE *stream) is a subroutine call which flushes the
buffer pointed at by 'stream'. It will not cause a trailing
new line, but will simply force the system to print out
what is currently pending in the buffer.)
3). Include a putchar('\n'); after the while loop, which will put
an extra newline after your '&' and before your next call to
output_msg().
I would choose number '2' above because it doesn't cause any extra newlines
or carriage returns to show up in your output, but it does cause the trailing
'&' to be printed and then it will wait with it printed until you print out
any more stuff. Because of the line-buffered nature of stdout, you will see
that you have a trailing '\n' in your first printf() line, which causes the
entire buffer to be flushed automatically.
> --
> /*******************************************************************/
> /* "Eritis sicut Deus, scientes * bvickers at bonnie.ics.uci.edu */
> /* bonum et malum" * --------------------------- */
> /*******************************************************************/
I hope this helps.
-- Gordon.
--
Gordon C. Galligher <|> ...!uunet!telxon!gorpong <|> gorpong at teleng.uucp.uu.net
Telxon Corporation <|> "Captain, I hardly believe that insults are within your
Akron, Ohio, 44313 <|> prerogative as my commanding officer" - Spock
(216) 867-3700 (3512)<|> (City on the Edge of Forever (Starring Joan Collins))
More information about the Comp.lang.c
mailing list