FFlush in C

Started by thiruvasagamani, Sep 22, 2008, 10:58 AM

Previous topic - Next topic

thiruvasagamani

Fflush(stdin):

It is used to flush\n in array before reading the string.It is predefined in <stdio.h>. We must call fflush() before gets().Stdin is standard input that is keyboard.Due to fflush() \n in array is flushed(deleted).Gets() is not executed in case fflush() is not called.Since array alraedy contain \n.

NOTE:
25 satya
30
40
50
from the above here fflush is not required because after 25 we are not pressed enter key then here fflush is not required.
25
satya
30
40
50
from the above,here fflush is required due to here we pressed enetr key afetr value 25 because after value if we press enter(\n) is stored in array then it gives not a desired result for that you have to flush(delete) the new line(\n) before you storing the string then you are using fflush(stdin).
eg:

printf(enter a value");
scanf("%d",a);
printf("enter employee name");
fflush(stdin);//flushes the \n in ename before storing a string in ename
gets(ename); //read astring in ename

25
satya (fflush using)
25 satya(fflush not required)
Thiruvasakamani Karnan