Well, this is not actually a question..
I have just occasionally found out that there’s an interesting way to declare local variables inside a switch/case block. Instead of using braces inside every case block, you can write:
switch (action) {
int res;
int value;
case ACTION_OPEN:
res = open(...);
...
break;
case ...
}
So, I just wonder what C/C++ compilers besides gcc support this construction? It looks like a common fall-through. Any comments on this construction are welcome!
Any standards-conforming C or C++ compiler will allow this . Even an old-fashioned (pre-ISO C99) C compiler will allow this, but only because the variable declarations are at the start of a block/compound statement (denoted by
{}).Note that what follows a
switchis almost a normal statement, except for the possibility of case labels:So in ANSI C89, it’s the braces that do the magic here.