留学国家排名知乎:被誉为C Bible一书中的一道题有些疑问~~

来源:百度文库 编辑:高考问答 时间:2024/05/15 15:04:07
就是统计digit的那里[代码中我加了注释的]
++ndigit[c-'0']; 这是什么东东?怎么理解
其他的我都懂~~

#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);
}

++ndigit[c-'0'];

就是把ndigit[c-'0'] 自加一

这不是个统计数么
c-'0' 是求位置
这是把数字的ascII字符转换为数字
如果c == '9'的话
就是
++(ndigit[9]);