GCC supports this as an extension.

Mixing declarations and code is allowed in C99 and C23 
will also allow placing labels before declarations and at
the end of a compound statement. GCC supports all this
also in earlier language modes.

See:
https://gcc.gnu.org/onlinedocs/gcc/Mixed-Labels-and-Declarations.html

You will get the warnings with -pedantic.

Martin

Am Donnerstag, dem 19.10.2023 um 07:39 -0400 schrieb Eric Sokolowsky via Gcc:
> I am using gcc 13.2 on Fedora 38. Consider the following program.
> 
> #include <stdio.h>
> int main(int argc, char **argv)
> {
>     printf("Enter a number: ");
>     int num = 0;
>     scanf("%d", &num);
> 
>     switch (num)
>     {
>     case 1:
>         int a = num + 3;
>         printf("The new number is %d.\n", a);
>         break;
>     case 2:
>         int b = num - 4;
>         printf("The new number is %d.\n", b);
>         break;
>     default:
>         int c = num * 3;
>         printf("The new number is %d.\n", c);
>         break;
>     }
> }
> 
> I would expect that gcc would complain about the declaration of
> variables (a, b, and c) within the case statements. When I run "gcc
> -Wall t.c" I get no warnings. When I run "g++ -Wall t.c" I get
> warnings and errors as expected. I do get warnings when using MinGW on
> Windows (gcc version 6.3 specifically). Did something change in 13.2?
> 
> Eric

Reply via email to