Question on printf()

Conor P. Cahill cpcahil at virtech.uucp
Fri Jun 8 13:02:06 AEST 1990


In article <24674.266e3b81 at kuhub.cc.ukans.edu> jian at kuhub.cc.ukans.edu writes:
>display()
>{ 
>   printf("\n\n\nEnter Command -> ");
>   alarm(60);
>}
>
>I don't know why the printf() only prints three linefeeds and the "Enter 
>Command -> " only shows up on every next call to display. It seems to me

The problem is caused by the fact that you are writing the data throught
the standard i/o buffer which for a tty is line buffered.  This means that
your data will not appear until a newline is output.

To fix this you could do any of the following:

1. use write(2) instead of printf.  writes are not buffered and the data
will be sent directly out to the screen.

2. add the line fflush(stdout) following the printf (although I would also
recommend that you then use fprintf(stdout,...) instead of printf() just so
that it is very plain what you are flushing).

3. add a setbuf(stdout,(char *) 0) at the begining of your program.  This 
turns off all output buffering and can have a detrimental effect on the
overall performance of your software.  

For what you are doing, #2 is probably the best solution.


-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 



More information about the Comp.unix.questions mailing list