What do you mean by buffer in C language?

CServer Side ProgrammingProgramming

A temporary storage area is called buffer. All input output (I/O) devices contain I/O buffer.

When we try to pass more than the required number of values as input then, the remaining values will automatically hold in the input buffer. This buffer data automatically go to the next input functionality, if it is exists.

We have to clear the buffer before the next input is taken in.


Following is the C program for buffer −

void main(){
   int a,b;
   printf("\n Enter a value: ");
   printf("\n Enter b value: ");
   printf("\n a+b=%d ",a+b);


When the above program is executed, it produces the following result −

Enter a value: 1
Enter b value: 2

Again, run the program. This time, we try to enter values in a and not in b.

Enter a value: 1 2 3
Enter b value: a+b=3

Even though we didn’t enter b value, it takes the previously stored value, which is present in buffer already.

In the implementation, when we need to remove standard input buffer data then go for flushall() or fflush() function.

  • flushall() − It is a predefined function present in stdio.h. by using flushall we can remove the data from I/O buffer.

  • fflush() − It is a predefined function in "stdio.h" header file which is used to clear either input or output buffer memory.

  • fflush(stdin) − It is used to clear the input buffer memory. It is recommended to use before writing scanf statement.

  • fflush(stdout) − It is used for clearing the output buffer memory. It is recommended to use before printf statement.

Published on 08-Mar-2021 14:20:04