2

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

这是代码示例:

#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,我得到:digits = 0 0 0 0 0 0 0 0 0 0, white space = 0, other = 0。来自 Xcode...

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

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

4

1 回答 1

1

从书中;

这个程序本身的输出是
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 的剪切粘贴可能会导致 2 个额外的空格:)

于 2013-01-13T01:12:20.200 回答