Linux manual recommends that the fflush
function should not be used.
So, I found that while( getchar() != '\n'
plays the same role as fflush(stdin)
.
e.g.)
my trial code:
#include <stdio.h>
void main(void)
{
char input[100] = {};
printf("abcd");
while(1);
}
If I execute the code above in Linux (Ubuntu), the result is nothing. Because \n
is not in string. So, I have to print them out by emptying stdout
buffer.
The strange phenomenon is that the result is printed well when I use either getc(stdout)
or getc(stdin)
.
#include <stdio.h>
void main(void)
{
char input[100] = {};
printf("abcd");
getc(stdout); // or getc(stdin); both working well.
while(1);
}
I don't know why both are done well. I expected that only getc(stdout)
should work well because I regard stdin
as keyboard buffer and stdout
as monitor buffer.