gpt4 book ai didi

char 上的 C 数学/整数运算

转载 作者:太空宇宙 更新时间:2023-11-04 04:23:24 25 4
gpt4 key购买 nike

<分区>

我目前正在阅读 K&R 书。第 22 页,数组 1.6

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

它说字符被认为是整数。我尝试删除第一个 if 语句中的“0”。但是,阵列停止工作。如果字符被认为是整数,为什么程序正常运行需要'0'

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com