Re: how to check the scanf function if it will read more than one number



Moosdau wrote:
> Richard Heathfield wrote:
> > For the same reason that pressing the handle on your toilet will not get rid
> > of the water waiting in your sink's tap (or faucet, if you're on that side
> > of the pond). Flushing is something we do to output, not to input.
>
> thanks.
> but what I want to know is the "actual" reason,
> not a "logical" one.

The "actual" reason is that the C language standard does not define
a behaviour for fflush() on an input stream.

> e.g.
> void f( int*p )
> {
> if (p->num)
> do something;
> }
> when this function is running, there will be an error.
> if I don't know the reason,what I want to know is :
> if p is a NULL pointer, p->num will cause the error.
>
> so ,the fflush function works very well on my computer now,

Your implementation probably defines a behaviour fflush on
input streams. It would do so as an extension. That extension
is not topical in comp.lang.c.

> If it cause a potential danger,
> I want to know what is it, and what is the condition.
> if possible, an example is the best. :)

void g(FILE *fp)
{
fflush(fp);
}

If fp is purely an input stream, this invokes undefined behaviour. In
other
words, it's as dangerous as f() above.

--
Peter

.



Relevant Pages

  • Re: scanf problem
    ... There is one function fflush(), which flushes out the i/o stream data. ... that's one possible meaning of "flushing" an input stream. ...
    (comp.lang.c)
  • Re: Forth input line buffered, why?
    ... whether interpreting or compiling waits for the user to type ... The main reason is that it's often desirable to manage the input stream ... I would hate to be unable to backspace on the ...
    (comp.lang.forth)
  • Re: C Interview Questions
    ... Simon Biber wrote: ... streams. ... which is another reason that there is no such thing as *the* correct ... argument to fflush. ...
    (comp.lang.c)
  • Re: function system() from stdlib.h
    ... fflush(stderr); ... Is there a specific reason for doing while(... ... ); other than readability? ...
    (comp.lang.c)
  • Re: Syntax der Bibliotheksfunktionen in der allg. Beschreibung
    ... # The fflush function flushes a stream. ... Da der C-Buchautor der Unterschied aufgefallen ist, ...
    (de.comp.lang.c)