×

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

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

4 minutes read.

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

4 minutes read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

Conditional Operator in C

In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is...

3 minutes read.

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.

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...

2 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

Exponential() in C

Introduction: In C language, there are available many types of functions. The exponential() function is one of them. It is a mathematical function. This article describes using the pow() property to...

3 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.