K
Keith Thompson
Please trim quoted material when you post a followup. I particular,
don't quote singatures unless you're actually commenting on them.
(A very minor point; there's no "@" in my name.)
The fact that fflush(stdin) invokes undefined behavior has been
pounded to death, so I won't mention it again. (Oops, I just did!)
But on a system that does define the behavior, namely Solaris, the man
page says:
Flushing an input stream discards any buffered input and adjusts
the file pointer such that the next input operation accesses the
byte after the last one read.
For input from a keyboard, that means (I think) that it discards
anything in the typeahead buffer, i.e., any characters that the user
has typed that have not yet been read by input operations. You *don't
know* that the input buffer contains only a '\n' character; it could
contain anything.
fflush(stdin) is undefined; when it is defined, it doesn't necessarily
do what you think it does.
don't quote singatures unless you're actually commenting on them.
@ Keith
(A very minor point; there's no "@" in my name.)
Actually you interpreted it wrongly. What I wanted to say was that
fflush(stdin) will flush out the whole buffer. But in this case the
buffer had only a "\n". So I wrote "fflush(stdin) just flushes out the
"\n" from the buffer".
Hope you got my point now.
The fact that fflush(stdin) invokes undefined behavior has been
pounded to death, so I won't mention it again. (Oops, I just did!)
But on a system that does define the behavior, namely Solaris, the man
page says:
Flushing an input stream discards any buffered input and adjusts
the file pointer such that the next input operation accesses the
byte after the last one read.
For input from a keyboard, that means (I think) that it discards
anything in the typeahead buffer, i.e., any characters that the user
has typed that have not yet been read by input operations. You *don't
know* that the input buffer contains only a '\n' character; it could
contain anything.
fflush(stdin) is undefined; when it is defined, it doesn't necessarily
do what you think it does.