C Quirk??

David Geary dmg at ssc-vax.UUCP
Thu Feb 4 06:40:41 AEST 1988


I've been wondering about this for a long time.  Maybe someone out there
can shed some light on the subject...

#include <stdio.h>
#include <ctype.h>

main()
{
  char ch;

  puts("Enter a Character:  ");

  while( (ch = getchar()) )
  {
    if( isupper(ch) )
      tolower(ch);

    printf("%c\n", ch);
    puts("Enter a Character:  ");
  }
}

Here's the output that I get when running the above program:

Enter a Character:
a
A
Enter a Character:


Enter a Character:


The first time, everything is ok.  The next time, however, it seems
to just blow right by the getchar().

This is what I think:

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()?

I love to program in C, but this p*sses me off.  Thanks in advance
for the replies.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~	"...You can't always get what you want..."	~
~			Rolling Stones			~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


-- 
***********************************************************
* David Geary, Boeing Aerospace Co., Seattle, WA 	  *
* (206)773-5249.  --I disclaim everything I've ever said--*
***********************************************************



More information about the Comp.lang.c mailing list