运行C - helloWorld运行但没有别的 - Ubuntu

时间:2014-05-25 19:15:29

标签: c ubuntu gcc runtime

我只是学习C语言(Java和Python很强)。

在编写像hello world这样的C程序时,我在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 book(Arrays部分)的Arrays部分复制的

感谢

1 个答案:

答案 0 :(得分:0)

这将一直运行直到检测到EOF。 EOF可以插入&#39;在Linux终端中按ctrl + D. 它工作正常。

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

btw:main的类型为int,应该返回一个值。

相关问题