I just discovered the following section in some code I maintain:
switch (m) {
case 62: { // opening
// some declarations
// do some stuff
break;
case 63:
// do some other stuff
break;
} // closing
default:
// default stuff
break;
}
The block opening is meant to declaring some local variables, but the closing brace is wrongly placed and occurs after the case 63.
I have never noticed this for months as it compiles well in Visual Studio 2010. I’ve tried debugging it and both cases work fine.
How can it be ? Is this correct C syntax ?
6.8.1 Labeled statements, C99
i.e. The curly braces have no effect on how the switch-case labels work but it merely creates a new scope.
This explains why the seemingly misplaced curly braces don’t result in a syntax error.