www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/05/11/05:05:23

From: XXguille AT XXiies DOT XXes (Guillermo Rodriguez Garcia)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Quirky getch()
Date: Sun, 09 May 1999 17:08:47 GMT
Organization: Telefonica Transmision de Datos
Lines: 32
Message-ID: <37368add.2424171@noticias.iies.es>
References: <37306B66 DOT BFCC4CDD AT yahoo DOT com>
NNTP-Posting-Host: iies229.iies.es
Mime-Version: 1.0
X-Newsreader: Forte Agent 1.5/32.451
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Hi

>Normally, we (at least I did :) would expect "Hello World" and "Sing
>along!" to be printed before the program pauses to wait for a keypress;
>The strange thing is, it doesn't.

Not strange at all. Read on...

>The program prints "Hello World" and
>then waits for a keypress before continuing to print "Sing along!". The
>problem can easily solved by replacing "getch();" with "Key = getch();"
>But why does it work this time? And the even stranger thing is that if I
>add a line feed i.e. "\n" at the end of the second output string, it
>works correctly too. Can anyone shed some light?

Standard output is line buffered; it won't print anything until it
finds an end of line character ("\n") or your program exits.

So it prints the first line, when it finds the "\n",  then it buffers
the second one, waits for your keypress and when it reaches the end of
the main function, it flushes the output buffer, thus printing the
second line. This is done for performance reasons (DJGPP uses DOS
calls to print text, and the protected mode to real mode switching is
slow, so the printing gets delayed as much as possible)

I don't know why it works when you use Key = getch(), though.

Regards: GUILLE

----
Guillermo Rodriguez Garcia
XXguille AT XXiies DOT XXes (ya sabes :-)

- Raw text -


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