Mail Archives: djgpp/1997/10/06/14:40:27
On 6 Oct 1997, Myknees wrote:
>
> Hello all.
>
> I am trying to learn C++, and so was delighted to find the djgpp compiler
> and the related software. My second program (source below) was designed to
> print a line, wait for input, and then print a second line and exit.
>
> When I compile it with the slightly crippled Borland Turbo C compiler,
> TCLite, it operates as expected.
>
> When I compile it with gcc.exe or from within RHIDE, then it doesn't run
> this way. Instead it starts off waiting for input from the console, and
> then when I press a key it prints _both_ lines and exits. i.e. It seems to
> be doing the functions out of order. Why?
>
> After further studying the FAQ, I tried using cprintf instead of printf,
> and everything worked as expected. Why?
>
> Is there a way that I can learn how about exactly what changes I'll need
> to make to code as I switch between these two compilers? I have looked in
> the docs and the FAQ.
>
>
> // This is based on a prog by M.L. Rinehart
>
> #include <stdio.h> // Necessary for printf function.
> #include <conio.h> // Necessary for getch function.
>
> int main()
>
> {
>
> printf( "line one before the getch " );
>
> getch(); // Get char from con for a pause.
>
> printf("line two after the getch ");
>
> return 0; // Tell OS no errors.
>
> }
> // End
>
>
[...]
>
> Thanks,
> --Ed (Myknees)
>
I know what it is: stdout in DJGPP is buffered. It means that when you try
to printf something, the text goes to the memory. It is printed onto the
screen when the memory buffer is full (and then the buffer is flushed).
You can prevent this by flushing the buffer manually:
printf...
fflush(stdout);
getch();
...
cprintf works without buffering because it doesn't use stdout.
- Raw text -