On 23 Jun 98, at 20:56, James wrote:

> ok, compile this code, then explain why it does what it does...
> ...
>     scanf ("%c", &c);
>     printf ("Char was [%c]\n", c);
>     printf ("Getc test\n");
>     printf ("Enter a character >");
>    c = getc (stdin);
> ...

scanf leaves out the newline character on the (input) stream, poor getc sees 
this newline char before the char you enter and reads it.

It is sometimes a bad idea to use scanf. Always using fgets and then retrieving 
the fields by sscanf is a better choice, although it requires a bit more typing.

Before anyone flames me, of course it's possible to use
    scanf("%c\n", &c);
and it still has many scanf disadvantages.

bye.


Reply via email to