我越来越从K&放大器的程序示例输出错误的; R第1.6 [英] I'm getting the wrong output from a program example in k & r chapter 1.6

查看:90
本文介绍了我越来越从K&放大器的程序示例输出错误的; R第1.6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有在同样的例子另一个问题,但对于不同的问题。

i know there is another question on the same example, but for different issue.

这是code例如:

#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);
}

我运行后,并与执行程序,我得到:位数= 0 0 0 0 0 0 0 0 0 0,空格= 0,其他= 0。从该X code ...

After I run is and execute the program with , I get: digits = 0 0 0 0 0 0 0 0 0 0, white space = 0, other = 0. From Xcode...

但他们说,这本书本身这个程序的输出是:位数= 9 3 0 0 0 0 0 0 0 1,空格= 123,其他= 345

But in the book they say "The output of this program on itself is: digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345"

你能告诉我什么在please..tnx下去。

Can you tell me whats going on please..tnx.

推荐答案

这本书;

该计划对本身的输出结果
  位数= 9 3 0 0 0 0 0 0 0 1,空格= 123,其他= 345

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

有自己的源代码作为输入尝试它;

Trying it with its own source as input;

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

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

关闭,但我怀疑从计算器的cut'n'paste可能占2个额外的空格:)

Close, but I suspect the cut'n'paste from StackOverflow may account for the 2 extra white spaces :)

这篇关于我越来越从K&放大器的程序示例输出错误的; R第1.6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆