Buffer in C


Prev Tutorial Next Tutorial

Buffer in C

Temporary storage area is called buffer.

All standard input output devices are containing input output buffer.

In implementation when we are passing more than required number of values as a input then rest of all values will automatically holds in standard input buffer, this buffer data will automatically pass to next input functionality if it is exist.

Example

#include<stdio.h>
#include<conio.h>

void main()
{
int v1,v2;
clrscr();
printf("\n Enter v1 value: ");
scanf("%d",&v1);
printf("\n Enter v2 value: ");
scanf("%d",&v2);
printf("\n v1+v2=%d ",v1+v2);
getch();
}

Output

Enter v1 value: 10 Enter v2 value: 20 V1+v2=30 Again run program Enter v1 value: 10 20 30 Enter v2 value: V1+v2=30
Buffer

Explanation:

In the above example we pass three input in v1 and we cannot pass any value in v2 but value of v1 is automatically pass in v2.

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

flushall()

it is a predefined function which is declared in stdio.h. by using flushall we can remove the data from standard input output buffer.

fflush()

it is a predefined function in "stdio.h" header file used to flush or 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 to clear the output buffer memory. It is recommended to use before printf statement.

Example

#include<stdio.h>
#include<conio.h>
void main()
{
int v1,v2;
clrscr();
printf("\n Enter v1 value: ");
scanf("%d",&v1);
printf("\n Enter v2 value: ");
fflush(stdin);
scanf("%d",&v2);
printf("\n v1+v2=%d ",v1+v2);
getch();
}

Output

Enter v1 value: 10 20 30 Enter v2 value: 40 v1+v2=50
Buffer
Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements