我是C的新手,我试图在xcode上运行这个示例代码,但它说构建失败了。我得到的代码与书中的代码完全相同,但它仍然无法运行。
#include <stdio.h>
/* count characters in input; 2nd version */
int main()
{
double nc;
for (nc =0; getchar() != EOF; ++nc) {
;
}
printf("%.0f\n", nc);
}
很抱歉,如果这是一个菜鸟问题。感谢所有帮助!
答案 0 :(得分:0)
问题代码缺少'return {int value}'语句。我添加了这个缺失的行,最后得到了以下代码:
#include <stdio.h>
/* count characters in input; 2nd version */
int main()
{
double nc;
for (nc =0; getchar() != EOF; ++nc) {
;
}
printf("%.0f\n", nc);
return(0);
}
以上编译时没有错误或警告使用:
gcc -Wall -o test test.c
要运行它,需要程序的输入来自文件。 我创建了一个名为'junk.txt'的文件,其中包含以下内容(不包括大括号):
[ adddsadsd 33334343434243]
然后我使用以下命令执行程序:
./test < junk.txt
生成输出:
25