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

## Example

Following is the C program for buffer −

#include<stdio.h>
void main(){
int a,b;
printf("\n Enter a value: ");
scanf("%d",&a);
printf("\n Enter b value: ");
scanf("%d",&b);
printf("\n a+b=%d ",a+b);
getch();
}

## Output

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

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

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 09:50:04