delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/06/14:40:27

Date: Mon, 6 Oct 1997 20:20:39 +0200 (MET DST)
From: Wojciech Piechowski <voyt AT delta DOT ds2 DOT pg DOT gda DOT pl>
To: Myknees <myknees AT aol DOT com>
cc: djgpp AT delorie DOT com
Subject: Re: Borland/djgpp functions
In-Reply-To: <19971006005401.UAA21318@ladder02.news.aol.com>
Message-ID: <Pine.GSO.3.96.971006201209.12051A-100000@delta.ds2.pg.gda.pl>
MIME-Version: 1.0


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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019