Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

fflush(stdin) meaning? in c

+19 votes
asked Oct 23, 2024 by amen abassi (220 points)

4 Answers

0 votes
answered Nov 18, 2024 by Peter Minarik (101,340 points)
0 votes
answered Nov 25, 2024 by Sudarshan Halse (140 points)

The purpose of fflush() is to clear or flush the buffer. However, according to the C standard:

fflush() is only guaranteed to work on output streams (like stdout).

0 votes
answered Feb 5, 2025 by Ralladoddi Raju (140 points)
Fflush is used for to clearing the buffers in the programmer or to avoid the buffer error in the in c language
+1 vote
answered Feb 10, 2025 by (160 points)
fflush(stdin) is used to clear the buffer off the input.

I recommend you to do it on a different way, though. Sometimes the compiler may not get the fflush(stdin) right.

So, when I need the buffer to be cleared, I open up a loop to see if the user provided input the way I wanted to.  In case of error, I use this one function:

while(getchar() != '\n')

There is a example of how to use it:

while(1){

        if (scanf("%d", &var) == 1){
            break;}

           
        else{
            while (getchar()!='\n');
            continue;}

}

Basically, just put it with the conditions you want to be pleased on the scanf and that's it.

Hope it makes sense!

If there is something wrong or missing, let me know!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...