If there is a variable declaration as the first statement after the colon at the end of a case expression, the compiler returns a syntax error. If there any statements between the case expression and the variable declaration then it will not produce an error, a partial list of the statements that I have found that will prevent an error are as follows: A void statement i.e. ((void) 0); Using an un-necessary code block i.e. { code} An empty line with nothing but a semi-colon ; Any function call It seems as long as there is SOMEHING in between the case expression and the variable declaration it is fine. See example code below: --- example.c --- void foo0(void) { int test = 0; switch (test) { case 0: int bar = 1; //error here: " example.c:8: error: syntax error before "int"" break; default: // break; } } void foo1(void) { int test = 0; switch (test) { case 0: int bar; //error here: " example.c:23: error: syntax error before "int"" bar = 1; //error here: " example.c:24: error: `bar' undeclared (first use in this function)" break; default: // break; } } typedef int UINT; //typedef for below void foo2(void) { int test = 0; switch (test) { case 0: UINT bar = 1; //error here: "example.c:41: error: syntax error before "bar"" break; default: // break; } } void foo3(void) { int test = 0; switch (test) { case 0: ((void) 0); //void statement int bar = 1; //works break; default: // break; } } void foo4(void) { int test = 0; switch (test) { case 0: { //code block int bar = 1; //works } break; default: // break; } } void foo5(void) { int test = 0; switch (test) { case 0: ; //empty line int bar = 1; //works break; default: // break; } } void foobar(void); //function prototype for below void foo6(void) { int test = 0; switch (test) { case 0: foobar(); //function call int bar = 1; //works break; default: // break; } } void foobar(void) //dummy function { //nada }
(In reply to comment #0) > If there is a variable declaration as the first statement after the colon at the > end of a case expression, the compiler returns a syntax error. That is exactly as expected. After the label, there needs to be a statement, and declarations are not statements.
Reopen to ...
Mark as a dup of bug 7508. *** This bug has been marked as a duplicate of 7508 ***