C Programming

What Does fflush(stdin) Do in C Programming

In C programming, the fflush(stdin) function is used to clear the input buffer memory of any data that may still be stored. It does this by freeing up any memory associated with the standard input stream, known as stdin, which is the default input used for reading any data from command line arguments or a keyboard. This means that any remaining data that hasn’t been processed is discarded and any data from the last successful input operation can be used.

According to the C standard, using fflush(stdin) is an unclear behavior pattern. Certain compilers, like Microsoft visual studio, allow this, though. Even when accepting an integer number with spaces and discussing the prior input for the same, a buffer is not cleaned over the next input. This problem is solved by flushing the same stream and buffer with fflush(stdin).

Let’s look at an example:

#include <stdio.h>
int main()
    char s[20];
    int i;
    for (i = 0; i<3; i++)
        printf("Enter a string\n");
        scanf("%[^\n]s", s);
        printf("%s\n", s);
    return 0;

The code above takes a single input and gives the same result. The buffer is then cleaned using the fflush(stdin) function, and then the next input is entered and printed and so on.


What Does fflush(stdin) Do in C Programming?

The fflush(stdin) function is used with input operations to ensure that data is properly processed, and nothing is left behind in the input buffer. In C programming, every input operation will leave some data behind in the input buffer. This data is left there until the program chooses to process it, but this can cause unexpected issues. For example, if a program is expecting a number as an input but the user accidentally types in a letter, the data left in the buffer will still be processed as the next input. This could cause the program to crash or the data to be incorrectly parsed.

Using the fflush(stdin) function can help prevent this from happening. The function clears the buffer manually and thus any remaining data is removed from it. Without this function, the computer would still process the leftover data as the next input but now any error can be caught, and the program can be made to re-prompt the user for the correct input.

Aside from using it to clear input buffers, the fflush(stdin) function can also be used to flush output streams. This is useful for programs that require a real-time output or display the progress of a program. By flushing the output stream, the most recent output can be displayed immediately. Otherwise, the program would need to wait until the entire stream is full before it can be displayed.


The fflush(stdin) function is an essential part of C programming. By flushing remaining data from input buffers, it helps prevent unexpected inputs from throwing off the program and causing errors. Additionally, it can be used to flush output streams which can be useful for displaying real-time information. While it is not used frequently, the fflush(stdin) function is an important part of any programmer’s toolkit.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.