C语言问题,希望能有解释 谢谢

来源:百度知道 编辑:UC知道 时间:2024/05/29 04:35:10
#include <stdio.h>
void main()
{
char k; int I;
for(i=1;i<3;i++)
{ scanf(“%c”,&k);
switch(k)
{ case ‘0’: printf(“another\n”);
case ‘1’: printf(“number\n”);
}
}
}
程序运行时,从键盘输入:01<回车>,程序执行后的输出结果是

i=1时 输入0 k='0'
switch('0') 所以执行 case '0' 输出 another换行
又因为case '0'没有break语句 所以继续向下执行case '1' 输出number换行 退出switch
i=2是 输入1 k='1'
switch('1') 执行 case'1' 输出 number换行 退出switch
i=3时 退出for循环
所以综上得结果为
another
number
number

another