gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:40:38 25 4
gpt4 key购买 nike

我是 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);
}

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

最佳答案

值“EOF”不等于换行符,即“\n”。 “EOF”是当您按下 ctrl+d、当标准输入(又名 getchar())来自键盘时或当您到达文件时发送的值,如果您从文件重定向标准输入(您将使用 ./count < 文件)。

关于C程序不读取输入,不给出输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9764674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com