gpt4 book ai didi

c - 我从 k & r 第 1.6 章的程序示例中得到了错误的输出

转载 作者:太空宇宙 更新时间:2023-11-03 23:53:23 25 4
gpt4 key购买 nike

我知道关于同一个例子还有另一个问题,但针对的是不同的问题。

这是代码示例:

#include <stdio.h>
/* count digits, white space, others */
main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;

for (i = 0; i < 10; ++i)
ndigit[i] = 0;

while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;

printf("digits =");

for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);

printf(", white space = %d, other = %d\n", nwhite, nother);
}

在我运行 is 并使用 Ctrl + D 执行程序后,我得到:数字 = 0 0 0 0 0 0 0 0 0 0,空白 = 0,其他 = 0。来自 Xcode...

但是在书中他们说“这个程序本身的输出是:digits = 9 3 0 0 0 0 0 0 0 1,white space = 123,other = 345”

你能告诉我发生了什么事吗..tnx.

最佳答案

来自书本;

The output of this program on itself is
digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345

尝试用它自己的源作为输入;

> gcc test.c
> ./a.out < test.c

digits = 9 3 0 0 0 0 0 0 0 1, white space = 125, other = 345

关闭,但我怀疑来自 StackOverflow 的 cut'n'paste 可能会导致 2 个额外的空白 :)

关于c - 我从 k & r 第 1.6 章的程序示例中得到了错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14299614/

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