基本 C 程序无输出
No output in basic C program
我一直在阅读 K&R 的第一章,但我遇到了第一个问题。
#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);
}
我在终端中的输出为零。我已经尝试了很多不同的变体,但似乎无法将其输出和数字。
非常感谢任何帮助!
此程序需要将输入文件传递给它,但它按原样运行。
gcc test.c -o test
echo "12345" > data
./test < data
digits = 0 1 1 1 1 1 0 0 0 0, white space = 1, other = 0
这是另一个输出:
echo "111 111 222" > data
./test < data
digits = 0 6 3 0 0 0 0 0 0 0, white space = 3, other = 0
问题是,您通常不会从控制台输入 EOF
。如 中所述,它通过将文件重定向到程序的 stdin
来工作,因为文件由 EOF
终止。由于整个文件然后被传递到 stdin
,程序通过 getch()
读取它,"end of the input" 被正确识别。
您只是无法通过控制台输入 EOF
,因此不会获得任何输出。 (实际上 EOF
可能有一些特殊的快捷方式)
顺便说一句,自从 K&R 问世以来的这些年里,我们已经学到了很多关于是什么让代码变得更好 reliable/maintainable。一些例子:
#include <stdio.h>
/* count digits, white space, others */
int main ( void ) { /* type explicitly */
int c; /* declare 1 variable per line */
int i;
int nwhite = 0; /* initialize variables when possible */
int nother = 0;
int ndigit[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
while ((c = getchar()) != EOF) { /* use brackets */
if ((c >= '0') && (c <= '9')) { /* explicit precedence is clearer */
++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]); /* 1 liner is ok w/{} */
printf(", white space = %d, other = %d\n", nwhite, nother);
return 0; /* don't forget a return value */
}
我一直在阅读 K&R 的第一章,但我遇到了第一个问题。
#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);
}
我在终端中的输出为零。我已经尝试了很多不同的变体,但似乎无法将其输出和数字。
非常感谢任何帮助!
此程序需要将输入文件传递给它,但它按原样运行。
gcc test.c -o test
echo "12345" > data
./test < data
digits = 0 1 1 1 1 1 0 0 0 0, white space = 1, other = 0
这是另一个输出:
echo "111 111 222" > data
./test < data
digits = 0 6 3 0 0 0 0 0 0 0, white space = 3, other = 0
问题是,您通常不会从控制台输入 EOF
。如 stdin
来工作,因为文件由 EOF
终止。由于整个文件然后被传递到 stdin
,程序通过 getch()
读取它,"end of the input" 被正确识别。
您只是无法通过控制台输入 EOF
,因此不会获得任何输出。 (实际上 EOF
可能有一些特殊的快捷方式)
顺便说一句,自从 K&R 问世以来的这些年里,我们已经学到了很多关于是什么让代码变得更好 reliable/maintainable。一些例子:
#include <stdio.h>
/* count digits, white space, others */
int main ( void ) { /* type explicitly */
int c; /* declare 1 variable per line */
int i;
int nwhite = 0; /* initialize variables when possible */
int nother = 0;
int ndigit[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
while ((c = getchar()) != EOF) { /* use brackets */
if ((c >= '0') && (c <= '9')) { /* explicit precedence is clearer */
++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]); /* 1 liner is ok w/{} */
printf(", white space = %d, other = %d\n", nwhite, nother);
return 0; /* don't forget a return value */
}