Whimsically, I initialized the following array c
in a switch statement fully expecting my compiler to say NO you can't do that but much to my surprise it compiled in MSVC, GCC and Clang. Online example
I'm assuming that the standard allows it, in which case my question is why? ... considering that declaration and initialization of non-arrays is NOT allowed in case statements.
int main()
{
char ch;
switch( ch )
{
case 'x':
//int a = 42; // NOT OKAY
break;
case 'y':
int b;
b = 42; // OKAY
case 'z':
int c[2] = { 0 , 1 }; // OKAY (Huh???)
break;
};
}
c
is totally valid in the context ofswitch(ch)
, but not visible outside.switch
scope... (alsoint a = 42;
should perfectly work) (... no reason to tag C++ here)c
in the case statement.