c - 运行 C — helloWorld 运行,但没有其他内容 — Ubuntu

标签 c ubuntu gcc runtime

我刚刚开始学习 C 语言(擅长 Java 和 Python)。

当编写 C 程序(例如 hello world)时,我在 ubuntu cmd 行上使用 gcc hello.c -o hello 编译它,然后通过 ./hello 运行它,效果很好。

问题:

当编写一个执行任何类型“工作”的文件(直接从 ANSI C 书籍复制的示例文件)时,程序完全符合要求,但随后就挂起......

有人对为什么会发生这种情况有任何建议吗?

非常感谢!

编辑~~

编译并运行的工作代码:

    #include <stdio.h>
    main()
    {
    printf("Hello,World\n");
    }

编译但在运行时挂起的代码:

    #include <stdio.h>
    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 psace = %d, other = %d\n", nwhite, nother);
    }

^^这是从 ANSI C 书的数组部分(数组部分)复制而来的

谢谢

最佳答案

这将运行直到检测到 EOF。 在 Linux 终端中按 ctrl+D 可以“插入”EOF。 那么就可以正常工作了。

while ((c = getchar()) != EOF) 更改为 while ((c = getchar()) != '\n') 即可只会要求您输入一行,然后返回结果。

顺便说一句:main 的类型为 int 并且应该返回一个值。

关于c - 运行 C — helloWorld 运行,但没有其他内容 — Ubuntu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23859007/

相关文章:

c - 设置 MPI_Info : value is too long 时出错

c - 如何将代码中的修改链接到正确的 FreeType 库版本?

ubuntu - Zoneminder 缺少 api 文件夹

c - 如何在 Linux 上从 Java 代码调用 C 函数

c - Linux 与 Windows 性能对比

macos - 如何在Mac OS X上将编译的目标文件(hello.o)与ld链接?

c - 将带有 char* 字符串的 C 结构保存到文件中

Clang 使用 var args 编译对 void 函数的调用

ubuntu - 在特定目录中打开 ubuntu 终端的更快方法

ubuntu - 是否有适用于 Android 的 ubuntu appium 检查器