Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uunet!virtech!cpcahil From: cpcahil@virtech.uucp (Conor P. Cahill) Newsgroups: comp.unix.questions Subject: Re: Question on printf() Message-ID: <1990Jun8.030206.4532@virtech.uucp> Date: 8 Jun 90 03:02:06 GMT References: <24674.266e3b81@kuhub.cc.ukans.edu> Reply-To: cpcahil@virtech.UUCP (Conor P. Cahill) Organization: Virtual Technologies Inc., Sterling VA Lines: 34 In article <24674.266e3b81@kuhub.cc.ukans.edu> jian@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