c - 当我在 Linux Mint 的终端窗口上编译具有 getchar() 等的程序时,我没有得到输出

标签 c linux char counting getchar

我对 Linux Mint 还比较陌生,并且有点想重新开始编程。我正在尝试使用 Brian W. Kernighan 和 Dennis M. Ritchie 编写的 C 编程来学习阅读、复制和计数的概念。

我理解行计数、字数计数等概念,但每当我运行代码时,我都不会在终端窗口上得到输出,而当我在 codechef 上使用在线编译器时却得到输出。

有人可以解释为什么会发生这种情况以及解决方案吗?

谢谢。 :)

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

最佳答案

你认为这有什么作用?

while ((c = getchar()) != EOF)

这里看起来你想执行代码直到 c == '\n'

关于c - 当我在 Linux Mint 的终端窗口上编译具有 getchar() 等的程序时,我没有得到输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33540311/

相关文章:

c - 三态元胞自动机规则是如何生成的?

c - 由于 asprintf 函数无法编译程序

c++ - 需要使用 linux ubuntu 从 .cpp 查找上次启动的时间?

c - 只有一个孙子被杀,而不是 5 个

java - 这可能吗? : HashMap<String, 字符>

c - 位提取和隐写术

c - Bison 解析器 : trouble in print token

linux - 可以将 AF_INET 套接字连接到 AF_UNIX 吗?

arrays - 如何将 int 数组转换为字符串数组

c++ - 当试图找到 '*' 程序发现 '.' insted in c++