【问题标题】:No output in basic C program基本 C 程序中没有输出
【发布时间】:2016-01-02 17:23:05
【问题描述】:

我一直在阅读 K&R 的第一章,但我遇到了第一个问题。

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

}

我在终端中的输出为零。我已经尝试了许多不同的变体,但似乎无法将其输出和数字。

非常感谢任何帮助!

【问题讨论】:

  • 第一个错误 K&R 太老了,不要为了学习而阅读。阅读它只是为了了解语言的历史。你有一个非常 K&Rish 的错误,main() 必须返回 int
  • ./a.out &lt; test.txt
  • 你能运行一个“hello world”程序吗?
  • 你的程序适合我。从 shell 传递输入时,您是否按 Ctrl-C 而不是 Ctrl-D
  • 它工作正常。输入内容并按ctrl+D(c = getchar()) != EOF 的含义是它读取您键入的字符并等待直到您按下 ctrl+D 并打印字符统计信息。

标签: c output


【解决方案1】:

这个程序需要一个输入文件来传递给它,但它按原样工作。

 gcc test.c -o test
 echo "12345" > data
 ./test < data

 digits = 0 1 1 1 1 1 0 0 0 0, white space = 1, other = 0

这是另一个输出:

 echo "111 111 222" > data
 ./test < data

 digits = 0 6 3 0 0 0 0 0 0 0, white space = 3, other = 0

【讨论】:

  • 我将 OP 的代码复制粘贴到 test.c 中,然后编译并运行它。
  • 抱歉,我阅读了需要输入文件,但没有看到./test &lt; data。请您提及不正确的main() 签名吗?
【解决方案2】:

问题是,您通常不会从控制台输入EOF。如Tony Ruths answer 中所述,它通过将文件重定向到程序的stdin 来工作,因为文件由EOF 终止。由于整个文件随后被传递到stdin,程序通过getch() 读取该文件,因此“输入结束”被正确识别。

您只是无法通过控制台输入EOF,因此无法获得任何输出。 (其实EOF可能有一些特殊的快捷键)

【讨论】:

    【解决方案3】:

    顺便说一句,自 K&R 面世以来的这些年里,我们学到了很多关于如何让代码更可靠/更可维护的知识。一些例子:

    #include <stdio.h>
    
    /* count digits, white space, others */
    
    int main ( void ) {                 /* type explicitly */
    
            int c;                      /* declare 1 variable per line */
            int i;
            int nwhite     = 0;         /* initialize variables when possible */
            int nother     = 0;
            int ndigit[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    
            while ((c = getchar()) != EOF) {          /* use brackets */
                    if ((c >= '0') && (c <= '9')) {   /* explicit precedence is clearer */
                            ++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]); /* 1 liner is ok w/{} */
            printf(", white space = %d, other = %d\n", nwhite, nother);
            return 0;                            /* don't forget a return value */
    }
    

    【讨论】:

    • 哇,谢谢你的例子。总是有人告诉我,K&R 是学习 C 的好方法。根据每个人的 cmet,我开始看到它对于今天的标准来说是史前的。感谢您的帮助!
    • @woodfordb 我认为您因为提出这个问题而陷入了非常不幸的境地,并且您现在已经开始有这种感觉。恕我直言,您绝对应该继续使用 K&R 并完成它。除了 K&R,我从不需要太多外部材料来学习 C。
    • @scarecrow 我肯定会继续使用 K&R。它似乎仍然是学习 C 语言的最佳方法之一。我只会更仔细地研究书中的问题。感谢您的洞察力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多