我刚刚开始学习 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/