delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/07/07:46:29

From: "A. Sinan Unur" <asu1 AT cornell DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Borland/djgpp functions
Date: Sun, 05 Oct 1997 21:29:13 -0400
Organization: Cornell University http://www.cornell.edu
Lines: 73
Sender: asu1 AT cornell DOT edu (Verified)
Message-ID: <34383EE9.42A83CA0@cornell.edu>
References: <19971006005401 DOT UAA21318 AT ladder02 DOT news DOT aol DOT com>
Reply-To: asu1 AT cornell DOT edu
NNTP-Posting-Host: cu-dialup-0028.cit.cornell.edu
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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?

because conio functions and stdio functions are different. printf is
line-buffered if stdout has not been redirected or if you have not
disabled buffering completely. in any case, it is not a good idea to
intermix stdio and conio functions this way.

> 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.

reading rather than looking helps.

> 
> // 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  " );

if you instead do 
        printf( "line one before the getch  \n" );
the buffer will get flushed before getch(). you can also do
fflush(stdout) if you do not want to print a newline.

> 
>         getch();        // Get char from con for a pause.

you could also use the stdio function getchar() rather than the conio
function.

> 
>         printf("line two after the getch  ");
> 
>           return 0;       // Tell OS no errors.
> 
> }
>                         // End
> 
> I'm not sure how helpful this information will be, but here is the 
>environment:

btw, you realize this is C, not C++, right?
-- 
----------------------------------------------------------------------
A. Sinan Unur
Department of Policy Analysis and Management, College of Human Ecology,
Cornell University, Ithaca, NY 14853, USA

mailto:sinan DOT unur AT cornell DOT edu
http://www.people.cornell.edu/pages/asu1/

- Raw text -


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