Hello,
        I'm no expert, but I've been bitten by this before. I think what
it is is that scanf leaves the carriage return in the keyboard buffer.
Then when you call getc it reads the carriage return and prints it out
like so:
([
]). 
If, however, you clear stdin of leftover characters everything works
fine. here is fixed code to do that.

/* Start Of Code */
#include <stdio.h>

char c;

void clear_kb();
int main ()
{
    printf ("Scanf Test\n");
    printf ("enter a character> ");
    scanf ("%c", &c);
    clear_kb();
    printf ("Char was [%c]\n", c);
    printf ("Getc test\n");
    printf ("Enter a character> ");
    c = getc (stdin);
    printf ("Char was [%c]\n", c);

        return 0;
}
void clear_kb()
{
        char junk[80];
        gets(junk);
}
/* End Of Code */
 


Joseph Martin
[EMAIL PROTECTED]
Linux newbie/sysadmin ( dangerous combo! ;-> )

On Tue, 23 Jun 1998, James wrote:

> ok, compile this code, then explain why it does what it does...
> 
> /* Start Of Code */
> #include <stdio.h>
> 
> char c;
> 
> int main ()
> {
>     printf ("Scanf Test\n");
>     printf ("enter a character >");
>     scanf ("%c", &c);
>     printf ("Char was [%c]\n", c);
>     printf ("Getc test\n");
>     printf ("Enter a character >");
>     c = getc (stdin);
>     printf ("Char was [%c]\n", c);
> 
>       return 0;
> }
> /* End of Code */
> 
> What does it do? It will let me input the first char, but never asks for the
> second. Why? and how can i make it ask for the second (or however many i ask
> it for).
> 
> incidentally, i've seen EXACTLY the same 'feature' in Assembly, Pascal and
> Modula 2!
> 
> -[[EMAIL PROTECTED]]-------------------------------------------------
> Http://x-map.home.ml.org                  Mailto : [EMAIL PROTECTED]
> ---[Quote Of The Day]----------------------------------------------------------
> Old programmers never die.  They just branch to a new address.
> -------------------------------------------------------------------------------
> 


Reply via email to