www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/11/13/03:45:30

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: wierd problem
Date: Thu, 13 Nov 1997 07:11:28 +0000
Organization: Two pounds of chaos and a pinch of salt
Lines: 45
Message-ID: <346AA820.48A9@cs.com>
References: <01bceff6$b2df4060$63206ccb AT ashod>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp204.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Ashod wrote:
> 
> I recently wrote this.
> 
> void main (void)
> {
> printf("\nHello");
> getch();
> }
> 
> // but it did it the wrong way round.... it displayed hello after I hit a
> key,
> any thoughts on this ??

You've tripped over another reason why it's bad to write books that use
nonstandard programming examples.  In your case, you are mixing stdio
and conio functions, which is guaranteed to have unpredictable results. 
In DJGPP, standard output (what printf writes to) is line-buffered,
meaning that it does not get displayed on the screen unless one of the
following occurs:

- You write a newline ('\n') to stdout.
- You call an input function that reads from stdin (scanf, gets,
getchar, etc.).
- You manually flush the buffer with fflush().

However, getch(), being a conio function, reads directly from the BIOS
keyboard, and does not trip the second clause above.

If you examine this, you'll see how to solve your problem.  More
information can be found in chapter 9.4 of the DJGPP FAQ.

P.S.:  void main() is illegal under ANSI C/C++.  main() must return an
integer.  I suggest that you burn your C book and get one that teaches
the language properly.  :)

hth

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "Always listen to experts.  They'll  |
|       aka Fighteer I       | tell you what can't be done, and why.|
|   mailto:fighteer AT cs DOT com   | Then do it."                         |
| http://www.cs.com/fighteer |                - Lazarus Long        |
---------------------------------------------------------------------

- Raw text -


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