main ()
{
int c;
while ((c = getchar()) != EOF)
{
putchar(c);
}
}
I know that getchar()
buffers the character. If i execute this program and input some character like Hello
and without pressing enter I pressed EOF
character why it printing Hello
again and asking for new character?
When you type the 'EOF' character, the terminal driver makes all the characters in the input buffer available to the program, even if you've not typed a newline. Since the code underneath
getchar()
got some characters, it wasn't at EOF yet. When you type the second 'EOF', there are no characters left to be sent (but the terminal driver lets theread()
function know there were 0 bytes available), so theread
gets 0 bytes returned, which indicates EOF.