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ようになります。数字= 0 0 0 0 0 0 0 0 0 0、空白= 0、その他=0。Xcodeから..

しかし、本の中で彼らは「このプログラム自体の出力は次のとおりです:数字= 9 3 0 0 0 0 0 0 0 1、空白= 123、その他=345」

何が起こっているのか教えてください..tnx。

4

1 に答える 1

1

本から;

このプログラム自体の出力は、
数字= 9 3 0 0 0 0 0 0 0 1、空白= 123、その他= 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 に答える