Ask your own question, for FREE!
Computer Science 25 Online
OpenStudy (anonymous):

why use fflush(stdin) in C programming language?

OpenStudy (anonymous):

that removes the data stored by the before programmes

OpenStudy (anonymous):

It clears the output buffer, so this not interfere wrong on the next operation. And in Input operations, it will write the data on the destiny and erase the buffer. A fast example, if you use a scanf operation, then a fgets, the fgets will only read the \n. If you use the fflush after the scanf, the fgets will work normally.

OpenStudy (shadowfiend):

I believe the specific confusion here is why you would flush standard input specifically. It looks like this is the kind of thing you'd do when you want to make sure recent input isn't still sitting in the buffer; e.g., if someone has entered a password. Another reason is if you want to flush data that you *haven't read yet*, but are no longer interested in. I can't think of a good use case for this at the moment. It's also non-portable, meaning there are some systems where it will not actually do anything at all.

OpenStudy (anonymous):

This serves to clear the buffer input and output of the computer so that your scanf (% d) does not take someone "typing rubbish" and revert back buffer without pro ana State 0

OpenStudy (anonymous):

Clearing stdin (the standard input) serves to remove all characters from the buffer. Why would you do this? Here's a typical Scenario: You have done several scanf statements to grab data from the input buffer and after doing this, a return character still remains in the buffer. Then this bit of code is run: int val = 0; printf("Please enter a value and press return: "); scanf("%d",&val); Now, in expected execution, the program is to wait for the user to press return before it moves on. But there's already a return character in the buffer. So instead of waiting, the program will treat the return character in the buffer as the user pressing return and move on; no value being assigned to the variable val. To avoid such a scenario, it is advised to use the fflush(stdin) statement to clear the buffer. Hope this helped. -- Sando University of Guyana

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!