TA的每日心情 | 汗 2024-10-15 10:05 |
---|
签到天数: 372 天 [LV.9]以坛为家II
|
楼主 |
发表于 2014-2-26 15:37:49
|
显示全部楼层
同样的问题在switch中也存在,比如以下代码:- #include <stdio.h>
- int main() {
- int a = 10;
- int b = 5;
- char c = '+';
- switch (c){
- case '+':
- int sum = a + b;
- printf("sum is %d", sum);
- break;
- case '-':
- int minus = a - b;
- printf("minus is %d", minus);
- break;
- }
- return 0;
- }
复制代码 case'+' 与 case'-' 两者是互斥关系,但在编译器看来,他们处于同一作用域(编译器通过{}来区分作用域),存在冲突,因而编译不通过,所以要以{}将代码块区分开来:- #include <stdio.h>
- int main() {
- int a = 10;
- int b = 5;
- char c = '+';
- switch (c){
- case '+':
- {
- int sum = a + b;
- printf("sum is %d", sum);
- break;
- }
- case '-':
- {
- int minus = a - b;
- printf("minus is %d", minus);
- break;
- }
- }
- return 0;
- }
复制代码
|
|