The following code produces a syntax error: [EMAIL PROTECTED]:/home/alala/price/temp>cat > test.c #include <stdio.h> int main(void) { int number = 1; switch (number) { case 1: int a = number; printf("%d\n", a); break; default: printf("No idea.\n"); } return 1; } [EMAIL PROTECTED]:/home/alala/price/temp>gcc -std=c99 test.c -o test test.c: In function `main': test.c:8: error: parse error before "int" test.c:9: error: `a' undeclared (first use in this function) test.c:9: error: (Each undeclared identifier is reported only once test.c:9: error: for each function it appears in.)
I am explicitly telling the compiler I want C99, so I should be allowed to declare anything anywhere. If I add braces around the contents of the case statement, the syntax error disappears and the program will compile. However, braces should be unnecessary within a case statement. Interestingly, the addition of an (effectively no-op) statement before the declaration will also convince it to compile: [EMAIL PROTECTED]:/home/alala/price/temp>cat > test.c #include <stdio.h> int main(void) { int number = 1; switch (number) { case 1: number *= 1; // dummy statement added int a = number; printf("%d\n", a); break; default: printf("No idea.\n"); } return 1; } [EMAIL PROTECTED]:/home/alala/price/temp>gcc -std=c99 test.c -o test [EMAIL PROTECTED]:/home/alala/price/temp>./test 1 This behaviour is observed under both gcc 3.4.3 and 3.2.3 (on a different machine). The 3.4.3 config is below. [EMAIL PROTECTED]:/home/alala/price/temp>gcc -v Reading specs from /usr/lib/gcc/x86_64-pc-linux-gnu/3.4.3/specs Configured with: /var/tmp/portage/gcc-3.4.3-r1/work/gcc-3.4.3/configure --enable-version-specific-runtime-libs --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/3.4.3 --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/3.4.3/include --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/3.4.3 --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/3.4.3/man --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/3.4.3/info --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/3.4.3/include/g++-v3 --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --disable-altivec --enable-nls --without-included-gettext --with-system-zlib --disable-checking --disable-werror --disable-libunwind-exceptions --enable-multilib --disable-libgcj --enable-languages=c,c++,f77 --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu Thread model: posix gcc version 3.4.3 20041125 (Gentoo 3.4.3-r1, ssp-3.4.3-0, pie-8.7.7) -- Summary: Declaration within case statement produces syntax error Product: gcc Version: 3.4.3 Status: UNCONFIRMED Severity: normal Priority: P2 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: price at ifa dot hawaii dot edu CC: gcc-bugs at gcc dot gnu dot org http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23365