Is fflush (stdin) correct?

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.