Search code examples
ceofgetcharputchar

getchar() in c while using it


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?


Solution

  • 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 the read() function know there were 0 bytes available), so the read gets 0 bytes returned, which indicates EOF.