www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/02/27/03:40:08

From: "A.Appleyard" <A DOT APPLEYARD AT fs2 DOT mt DOT umist DOT ac DOT uk>
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Date: Mon, 27 Feb 1995 08:14:53 GMT
Subject: Re: Question reagarding kbhit()

jkeene AT unlinfo DOT unl DOT edu (Jon Keene) wrote (Subject: Question regarding
kbhit()):-

> Could anyone tell me why it is that when I run the following program, it
> pauses for the keyboard hit _before_ doing any output?
#include <stdio.h>
#include <pc.h>
#define BUFSIZE 1840  /* (80 * 23) */
#define BLANK ' '
main () {int i, buf[BUFSIZE];
        for ( i = 0; i < BUFSIZE; i++ ) buf[i] = BLANK;
        buf[0] = 'a';
        buf[BUFSIZE - 1] = 'z';
        for ( i = 0; i < BUFSIZE; i++ ) printf("%c", buf[i]);
        while (!kbhit()) ;
        return 0;}

printf() (formatted print to screen) stores the output characters in a buffer
and only sends the bufferful to the screen when it is told to print a '\n'.
`fflush(stdout);' called after each fprint() call will empty FILE* stdout's
buffer and thus cause the behavior that you seem to want.

- Raw text -


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