Mail Archives: djgpp/1999/05/11/05:05:23
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 -