Given the following code:
#include<stdio.h>
int main()
{
int a=1;
switch(a)
{ int b=20;
case 1:
printf("b is %d\n",b);
break;
default:
printf("b is %d\n",b);
break;
}
return 0;
}
What do you think can be value of b
in both of these printf
statements? Of course, it is not 20.
In C++ the code is ill-formed. Case-label 1
crosses initialization of b
.
In C99 it is valid but invokes Undefined Behaviour.
精彩评论