I think what you're saying is you would make those two calls to setvbuf
after a call to scanf
, and that on at least one C implementation it had the effect of discarding any "left over" characters, that were not consumed by scanf
, in stdin's buffer.
This is not a good way to discard "left over" characters, because the C standard says that setvbuf
"may be used only after the stream has been associated with an open file and before any other operation (other than an unsuccessful call to setvbuf) is performed on the stream". (N1570 §7.21.5.6p2.) Violation of this rule should only cause setvbuf to fail, but nonetheless that means it doesn't do what you want.
The best way to do what you want is to not use scanf
. Instead, write a real parser for your input and feed it either character by character (using getchar
) or line by line (using getline
or fgets
). A real parser has principled handling of white space (including newline characters) and syntax errors, and therefore never finds itself with "left over" input.
Writing real parsers is unfortunately a book-length topic, so I will say only that sscanf
is never useful in a real parser either. (Standard library functions that are useful include strsep
, strchr
, and the strto*
family.)
There are rare circumstances (usually involving processing of interactive input) where you really do have to discard junk at the end of a line. In those circumstances, the way to do it is with this loop:
int c;
do c = getchar();
while (c != '\n' && c != EOF);