Fflush (stdin) has never been defined in the C and C ++ standards ). Some people may say, "but I used fflush (stdin) to solve this problem. How can you say it is wrong ?" Indeed, some compilers (such as vc6) Support fflush (stdin) to clear the input buffer, but not all compilers must support this function (GCC in Linux does not support this function ), because fflush (stdin) is not defined in the standard at all ). The msdn document clearly states that fflush on input stream is an extension to the C standard (the fflush operation input stream is an extension of the C standard ). Of course, if you don't care about program portability, using fflush (stdin) is no big problem. The following is the definition of fflush function in c99:
Int fflush (File * stream );
If the stream points to the output stream or update stream, and the update stream
If the most recent operation is not input, the fflush function transfers any data to be written in the stream
Host environment writes data to a file. Otherwise, its behavior is undefined.
The original article is as follows:
Int fflush (File * stream );
If stream points to an output stream or an update stream in which
The most recent operation was not input, the fflush function causes
Any unwritten data for that stream to be delivered to the host environment
To be written to the file; otherwise, the behavior is undefined.
The host environment can be understood as the operating system or kernel.
From this we can see that if stream points to the input stream (such as stdin), then the behavior of the fflush function is uncertain. Therefore, using fflush (stdin) is incorrect, at least because of poor portability.
(From csdn Forum http://bbs.csdn.net/topics/330219044)