If I input abc
as the text stream, getchar()
reads it and putchar
prints it. I used printf
statements to trace whats exactly happening. But I get a strange output after I print the characters of input text stream. It enters once more the while
loop and execute those two printf
statements i.e when i
is 4
. Does this get my Enter key as an input. If "yes" I also tried using fflush
but that creates yet another issue. No character gets printed after I fflush
creating problems with getchar
. Why is this happening and how to fix it?
What happens if I use fflush(stdin)
#include <stdio.h>
main(){
c=getchar();
fflush(stdin);
while(c!= EOF)
{
putchar(c);
c=getchar();
fflush(stdin);
}
}
I write ABC
on console when using fflush
. It just getchar()
'a' and prints it. It doesn't even get the remaining characters.
Below it is the main program that I was talking about before I used fflush
.
#include <stdio.h>
main()
{
int c,i=1;
c=getchar();
while(c!= EOF) // while(c=getchar!=EOF) putchar(c);
{
printf("\n\nthis is upper %d time in loop i.e. before the putchar\n\n",i);
putchar(c);
printf("\n\nthis is down %d time in loop i.e. after the putchar\n\n",i);
c=getchar();
i++;
}
}