scanf(..)

Henry Spencer henry at utzoo.uucp
Sun Jun 4 16:41:42 AEST 1989


In article <225800176 at uxe.cso.uiuc.edu> burkie at uxe.cso.uiuc.edu writes:
>scanf("%c", &c);
>...
>I find that sometimes the program above doesn't stop
>at the scanf to wait for input, esp. if there are other
>input, print etc. statements.. Do I have to flush some 
>buffers etc for this to work properly?

There is often buffering of input; in particular, many systems (all Unix
systems, in particular) essentially do input a line at a time regardless
of whether you're only asking for it a character at a time.  Actually
inputting a character at a time is complicated and somewhat system-dependent;
queries on how it should be done should go to comp.unix.questions (or other
appropriate operating-system group), not comp.lang.c.

>while (...) {
>	scanf("%d, %d", &x, &y);
>
>if I type 'a' on the keyboard, why
>does the loop keep running, skipping over the scanf ?

Because the scanf is failing.  You should NEVER NEVER NEVER do a scanf
without checking the returned value to find out whether it worked.
-- 
You *can* understand sendmail, |     Henry Spencer at U of Toronto Zoology
but it's not worth it. -Collyer| uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.lang.c mailing list