C Quirk??

Mike DeCorte mrd at sun.soe.clarkson.edu
Fri Feb 5 15:52:04 AEST 1988


In article <1653 at ssc-vax.UUCP> dmg at ssc-vax.UUCP (David Geary) writes:

   It asks me to "Enter a Character:  ".  I type 'a', and then a newline.
   getchar() grabs the 'a' from stdin, but leaves the newline hanging.
   The next time getchar() comes around, it grabs the newline that was
   left hanging around last time.

   Note that if I insert fflush(stdin) before I do if( isupper(ch) ),
   everything works correctly.

   Is this a *bug* in C, or is it a *feature*.  Am I interpreting events
   correctly?  Is there a fix besides fflush()?

First off. You have the line
      tolower(ch);
in your text as a statement.  This does nothing.  tolower()
(as well as all of the things in ctype.h) return values.  In
this case it returns the lower case of ch but ch itself is not
modified.

second:
You are 100% correct on what C is doing with your input.
You see getchar gets ONE character, not one character and a
'\n'.  The first call grabs 'C' the second call grabs '\n'.
The logical response to this is "well how do I tell this
thing that I don't want to type return?"  What the tty driver
is doing is buffering your I/O.  To turn off buffering
is well system dependent.  On unix one uses ioctl(2) on 
anything else - I don't know.  

-- 

Michael DeCorte // mrd at clutx.clarkson.edu // mrd at clutx.bitnet
(315)268-4704 // P.O. Box 652, Potsdam, NY 13676



More information about the Comp.lang.c mailing list