b/C + + misunderstanding two: Fflush (stdin)

Source: Internet
Author: User
Tags definition stdin

1. Why Fflush (stdin) is wrong

First, look at the following programs:

include <stdio.h>

int main( void )

{

int i;

for ( ;;) {

fputs("Please input an integer: ", stdout);

scanf("%d", &i);

printf("%d\n", i);

}

return 0;

}

The program first prompts the user for an integer, then waits for the user to enter, and if the user enters an integer, the program prints out the integer just entered and prompts the user to enter an integer again, and then waits for the user to enter. But once the user input is not an integer (such as decimal or letter), assuming the scanf function the last time the integer is 2, then the program will continue to output "please input a integer:2." This is because scanf ("%d", &i) can only accept integers, and if the user enters a letter, the letter is left in the input buffer. Because there is data in the buffer, so the scanf function will not wait for user input, directly to the buffer to read, but the buffer is the letter, the letter is left in the buffer again, so repeatedly, resulting in non-stop output "Please input a integer:2".

Someone might say, "That's it, then add ' fflush ' to the scanf function (stdin)," and just empty the input buffer. "Yet this is wrong!" Fflush (stdin) has never been defined in the C and C + + standards. Some people may say, "but I used Fflush (stdin) to solve the problem, how can you say it is wrong?" "Indeed, some compilers (such as VC6) support the use of fflush (stdin) to empty the input buffer, but not all compilers support this feature (GCC is not supported on Linux) because there is no definition of fflush (stdin) in the standard." The MSDN documentation also clearly says that the fflush on input stream is a extension to the C standard (Fflush operation input Stream is the extension of the C standard). Of course, if you don't care about the portability of the program, there's no big problem with Fflush (stdin). The following is the definition of the Fflush function by C99:

int fflush (FILE *stream);

If the stream points to an output stream or to an update stream (update stream), and the most recent operation performed by this update stream is not input, then the Fflush function will transfer any pending data in the stream to the hosting environment (host environment) to write to the file. Otherwise, its behavior is undefined.

The original text reads as follows:

int fflush (FILE *stream);

If stream points to a output stream or a update stream in which the most recent operation is not input, the fflush func tion causes any unwritten data for that stream to is delivered to the host environment to is written to the file; Otherwis E, the behavior is undefined.

The host environment can be understood as an operating system or kernel.

So, if the stream points to an input stream (such as stdin), then the behavior of the Fflush function is indeterminate. Therefore, the use of Fflush (stdin) is not correct, at least the transplant is not good.

Related Article

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.