Mail Archives: djgpp/1997/03/12/11:54:32
Stefano Bizzi (sbcinf AT mbox DOT vol DOT it) wrote:
: I have a little problem compiling a simple program...
: With a code like this:
: ....
: printf("\nHello World");
: printf("\nSomething else...");
: clrscr();
: .....
: The compiler execute the clrscr() function BEFORE the last printf().
: Then, it writes "Hello World", it clear the screen and, then, write
: "Something else". Why? (I compiled the from RHide).
You are mixing commands from stdio.h and conio.h, which is generally
not a good idea. printf() sends to stdout, which is line buffered. This
means that nothing gets printed until you send a \n character. If you
stick to stdio.h routines, this isn't a problem because, for instance,
scanf() will clear the buffer before waiting for input. However, you
are using clrscr() which is a conio.h function, and so the buffer is
not flushed.
You have two main options here:
1) Put fflush(stdout) before clrscr() to manually flush the buffer
2) Only use conio.h routines (i.e. use cprintf() instead of printf())
HTH
--
George Foot <gfoot AT mc31 DOT merton DOT ox DOT ac DOT uk>
Merton College, Oxford.
- Raw text -