gpt4 book ai didi

c - c 中的失败案例是如何工作的?

转载 作者:行者123 更新时间:2023-11-30 21:29:04 24 4
gpt4 key购买 nike

#include <stdio.h>

main() /* count digits, white space, others */
{
int c, i, nwhite, nother, ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; i++)
ndigit[i] = 0;
while ((c = getchar()) != EOF) {
switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
ndigit[c-'0']++;
break;
case ' ':
case '\n':
case '\t':
nwhite++;
break;
default:
nother++;
break;
}
}
printf("digits =");
for (i = 0; i < 10; i++)
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n",
nwhite, nother);
return 0;
}

问题 1 - 如果 c = 0,那么控件是否直接转到语句并中断,或者它是否记住 c = 0 并继续下降,直到语句为止?这些情况如何工作?

#include <stdio.h>

int main()
{
int i = 0;
for(i = 0; i < 20; i++)
{
switch(i)
{
case 0: i+=5;
case 1: i+=2;
case 5: i+=5;
default: i+= 4;
break;
}
printf("%d ", i);
}
return 0;
}

问题 2 - 在这样的程序中,为什么在情况 1 中 2 相加? i 的当前值将是 5?如果一种情况为真,控件是否会忽略所有剩余/下级情况常量表达式,直到找到中断为止?

最佳答案

c 中的 switch 跳转到具有匹配值的 case,然后从那里执行所有代码,直到找到中断或 switch 结束。在第二个例子中,值 0 的儿子所有其他情况也被评估。如果你不想这样,你需要休息一下。

关于c - c 中的失败案例是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34904011/

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