gpt4 book ai didi

c - 为什么没有输出,EOF 的值是多少?

转载 作者:行者123 更新时间:2023-11-30 19:27:20 26 4
gpt4 key购买 nike

我正在测试《C 编程语言》一书中的代码,我想知道 EOF 的值是什么以及如何找到它?我尝试使用某种指针引用该值,但我只是得到一个 const 指针错误。无论如何,下面是我的代码,我想知道为什么点击输入后没有输出:

#include <stdio.h>

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

/* count lines, 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);
}

最佳答案

#include <stdio.h>

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

/* count lines, words, and characters in input*/
int main(void)
{
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 = %d\n", EOF);
return 0;
}


关于c - 为什么没有输出,EOF 的值是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55876072/

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