C printf() 不返回值

C printf() not returning values

我有一个非常简单的 C 示例程序,它对输入的字符、单词和空格进行粗略计数。该程序编译没有错误,但在测试时该程序没有通过 print 函数 return 任何 int 变量。我正在使用 VS2012 进行编码和编译。单步执行代码表明正在正确计算这些值。我的代码或编译器有问题吗?

#include <stdio.h>

#define IN 1
#define OUT 0
/* count digits, white space, others */
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);
}

你的循环是永久性的。意思是它没有结束..所以它没有到达 printf.

的电话

EOF 指标在这里显然不起作用。您必须打破特定键上的循环。比如回车,回车return的字符十进制表示为13。 NL 10。

如果你 运行 它与来自文件 a.exe < test.txtstdin 一起使用,它就会工作。如果您 运行 它与 stdin 来自 Linux 上的控制台,它会起作用。如果您 运行 它与 stdin 来自 Windows.

的控制台,则它不起作用

这一定是某种 Windows 控制台异常。

为了查看实际输出,请使用 Ctrl-F5 启动调试器。这将使控制台 window 保持打开状态。

有关详细信息,请参阅 this answer