switch_case_的高级用法 …
case 后面跟三个点号, 表示范围,范围是包含关系。
代码如下图
#include <stdio.h>
int main(int argc , char * argv[])
{
unsigned int judge = atoi(argv[1]);
switch (judge)
{
case 1 ... 2:
printf("run here \n");
break;
default:
printf("default \n");
break;
}
}
输出结果:
tan@tan:~/testCheck$ ./a.out 2
run here
tan@tan:~/testCheck$ ./a.out 3
default
总结
… 表示case 得范围,是闭区间,可以合并相同的行为。