【发布时间】:2015-02-24 20:52:16
【问题描述】:
我正在阅读 The C Programming Language 2nd Edition。我在教程介绍中做 2.10。我必须编写一个关于数组的程序。它应该计算数字、空格和其他。这是程序:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
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);
return 0;
}
按照书上的说法,程序本身的输出是
数字 = 9 3 0 0 0 0 0 0 0 1,空格 = 123,其他 = 345
我有两个问题:
- 如果我不按 CTRL+Z,程序将如何自行输出?
- 当我手动执行时,输出不正确。请查看我是否在代码中犯了错误。
我得到的输出是
数字 =,空格 = 0,其他 = 0
【问题讨论】:
-
1)
prog < prog.c2)i < 0-->i < 10 -
编译所有警告和调试信息(例如
gcc -Wall -Wextra -g,如果使用GCC...)然后使用调试器(例如gdb)。这是一项必不可少的技能。 -
我在调试器或构建文件中没有错误。我正在使用代码块
-
@UddhavaSwaminathan:那么请去寻求有关您故障调试器的帮助;很明显你非常需要它。别再骂钉子了;修好锤子!
-
@Ruud 他可能假设调试器是一个程序,它为您提供代码中的问题列表。 :-)
标签: c