C程序不读取输入,不给出输出

1 c input

我是C的全新人,并且在C编程语言书中有这个例子:

#include <stdio.h>

#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */

/* count line and words and characters in input */

main()
{
    int c, nl, nw, nc, state;

    state = OUT;
    nl = nw = nc = 0;
    while ((c = getchar()) != EOF)  {
            ++nc;
            if (c == '\n')
                    ++nl;
            if ( c == ' ' || c == '\n' || c == '\t')
                    state = OUT;
            else if ( state == OUT) {
                    state = IN;
                    ++nw;
            }
    }
    printf("%d %d %d\n", nl, nw, nc);
}
Run Code Online (Sandbox Code Playgroud)

代码在终端中用以下代码编译好:gcc -o count count.c当我运行它时:./ count它让我测试它,给我一个新行来输入输入.我输入,点击Return,它为我的文本输入提供了另一个空白行,而不输出任何内容.我做错了什么?我正在使用输入做一些其他的例子,我正在接收输出,但是我在本书中使用的代码现在都不适用于我.非常感谢.

mfr*_*kli 5

值"EOF"不等于换行符'\n'."EOF"是当您按下ctrl + d时发出的值,当标准输入(也称为getchar())来自键盘,或者当您到达文件时,如果您从文件重定向标准输入(您将使用./count <文件).