Why does getchar() recognize EOF only in the beginning of a line?

后端 未结 4 1438
情深已故
情深已故 2021-01-05 12:14

This example is from the K&R book

#include


main()
{
    long nc;

    nc = 0;
    while(getchar() != EOF)
        ++nc;
    printf(\"%ld         


        
4条回答
  •  無奈伤痛
    2021-01-05 12:24

    The program will read EOF only at the actual end of the input. If your terminal/OS/whatever only permit files to end at the start of a line then that's where you'll find them. I believe this is a throw-back to old-fashioned terminals where data was only transmitted a line at a time (for all I know it goes back to punched card readers).

    Try reading your data from a file that you've preprepared with an EOF mid-line. You may even find that some editors make this difficult! Your program should work fine with that as input.

提交回复
热议问题