Since the behavior of
fflush is not defined by the standard for the input flows, it is only sure that it works for the outgoing flows (sending the remaining content of the buffer to the output). Therefore
fflush should not use it.
It is also not advisable to use
fpurge since it is not defined in standard C nor is it portable to all platforms (as they have explained in other comments). In fact, in Linux it is not available, although there is a
void __fpurge(FILE *stream) function that does the same and is inside the standard GNU library (glibc).
The possible solution is to manually write the code that cleans the input buffer until you find a line break. The two options you have are:
while ((c = getchar()) != '\n' && c != EOF);
and the one that seems best to me:
In the last option, you are telling
scanf first to ignore (use of *) any number of non-characters blank (other than tab
'\t' , space
' ' or line break
'\n' ) with the
%*[^\n] specifier until you find a blank character (the line break), in which case ignore it with
%*c can not be replaced by
\n because in that case you would be ordering at that point to ignore any number of blank characters until you find one that is not, causing the scanf function to remain pending that you enter any non-blank character, which will not be read and will remain pending in the input buffer for the next call to
Keep in mind that these solutions are valid in the case that the buffer has content other than blank characters (
' ', '\t' o '\n' ). If the buffer is empty, it will wait for keyboard data other than blank characters to be entered (data that will be ignored).