×

Use of fflush(stdin) in C

Use of fflush(stdin) in C

Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into the terminal (in the case of stdout) or disk (throughout the case of file output). Its syntax is below.

Syntax:

The syntax for the fflush function in the C Language is:

int fflush(FILE *stream);

Can we use it for streaming inputs like stdin?

Using fflush(stdin) is an unspecified behavior pattern, as per C standard. However, some compilers like the visual studio in Microsoft permit this. How are those compilers used in these? A buffer does not get cleared over the next input even when taking an integer number with spaces and discusses the previous input for the same. fflush(stdin) is used to resolve this issue to clear this same stream and buffer.

Example 1:

/* C program for condition demonstration
in which flush() is only necessary
In some compilers, */
#include <stdio.h>
#include<stdlib.h>
int main()
{
    char str[15];
    int k;
    for (k=0; k<5; k++)
    {
        scanf("%[^\n]s", str);
        printf("%s\n", str);
        // fflush(stdin);
    }
    return 0;
}

Output:

Use of fflush(stdin) in C

The above code only needs to take a single input and produces a certain result for the second input. The reason is that the string has been stored in the buffer, i.e., the stream also isn't cleared yet as the string with spaces or new line was expecting. So fflush(stdin) is used to manage this case.

Example 2:

/* C illustration program with flush(stdin)
The above program just worked perfectly
In some compilers, such as visual studio
Windows. */
#include <stdio.h>
#include<stdlib.h>
int main()
{
          char str[24];
          int k;
          for (k = 0; k<6; k++)
          {
                   scanf("%[^\n]s", str);
                   printf("%s\n", str);
                   // used to clear the buffer
                   // and accept the next string
                   fflush(stdin);
          }
          return 0;
}

Output:

Use of fflush(stdin) in C

Example 3:

#include <stdio.h>
#include <string.h>
int main () {
   char buffer[1024];
   memset( buffer, '\0', sizeof( buffer ));
   fprintf(stdout, "Going to set full buffering on\n");
   setvbuf(stdout, buffer, _IOFBF, 1024);
   fprintf(stdout, "This is www.tutorialandexample.com\n");
   fprintf(stdout, "This output will go into buffer\n");
   fflush( stdout );
   fprintf(stdout, "And this will come up during programming\n");
   fprintf(stdout, "will come after sleeping 10 seconds\n");
   sleep(10);
   return(0);
}

Now let us compile the above program and execute it, which will generate the desired results. (So this program continues buffering into the output in buffer until it encounters the first call to fflush() after which the output is buffered again and rests for 10 seconds at last. Before the program comes out, it sends the remaining output to the STDOUT.

Output:

Use of fflush(stdin) in C


Related Topics

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.

How to use sine() function in C

What is Function? The function is a set of statements. It takes input and performs some computation to produce output. The process is a set of codes only achieved when it is...

3 minutes read.

Pointer in C

Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr Symbol Description & (ampersand sign) Address of an operator...

1 minute read.

Limitations of Synchronisation and Uses of Static Synchronisation in Multithreading

The multithreading component of java is the element around which the idea rotates as it permits simultaneous execution of at least two program pieces for the most significant usage of...

9 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

GPA Calculator in C

GPA stands for Grade Point Average. This GPA is used to measure the student's academic performance in educational institutes. By using this, segregation takes place and lets us know how...

4 minutes read.

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function. What is gotoxy() function? In C language, gotoxy() is a pre-defined...

3 minutes read.

Math functions in C

Math functions in C: In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. The <math.h> header defines various mathematical...

3 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.