getchar()

Dave P. Schaumann dave at cs.arizona.edu
Wed Mar 6 06:11:07 AEST 1991


In article <1991Mar5.063644.8459 at cs.mcgill.ca> storm at cs.mcgill.ca (Marc WANDSCHNEIDER) writes:
>[program that reads input using getchar() deleted]
>
>   Echos every character that you put on the screen.  However, it does not 
>stop (even after a Capital A) until you hit return.

The problem with your program is that stdin (and stdout) is buffered.  This
means for input, that when you request a character, the system actually gets
a whole line, and then feeds you the result one character at a time.  This
is efficient for when the overhead on a read is high, and doesn't depend much
on the size of the input read.

That is why your program doesn't quit until you hit return; the call to
getchar() doesn't return until you  hit (carriage) return.

Your next question, no doubt is "how do I do unbuffered reads?".
Unfortunately, there is no portable way to do this, although many (most?)
systems have some way to do it.  I would suggest you RTFM to find out more.

-- 
		Dave Schaumann		dave at cs.arizona.edu
'Dog Gang'!  Where do they get off calling us the 'Dog Gang'?  I'm beginning to
think the party's over.  I'm beginning to think maybe we don't need a dog.  Or
maybe we need a *new* dog.  Or maybe we need a *cat*! - Amazing Stories



More information about the Comp.lang.c mailing list