-2

As per the link C Switch-case curly braces after every case , the below code should not compile since the variable is declared within switch without braces.

        case 2:
    
            int s = 0;
            printf("enter side value:");
            scanf("%d", &s);

            printf("area of square is %d", squarearea(s));
        break;

However lot of online c editors ( https://www.onlinegdb.com/online_c_compiler , https://www.programiz.com/c-programming/online-compiler/ ) are allowing this program to compile and run .

Why is it so ? These online compilers are claiming to use gcc but then why is this difference in behavior?

Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
lives
  • 1,243
  • 5
  • 25
  • 61
  • 1
    The answers to that linked question do not say the braces are required: it is a presumption of the OP. Some compilers might not like the variable definition to immediately follow a label, and might need additional syntax. – Weather Vane May 27 '23 at 12:27
  • The braces are not needed in C. Note that all variables created in each case will exists at the end of the switch and only them in the taken path will be initialized. It's only in C++ where the braces are mandatory, in C it's only a recommendation for clarity – dalfaB May 27 '23 at 12:28
  • Better use braces before the variable initialization/declaration and the `break;` statement. Correct scoping is significant in c an c++ as well! – πάντα ῥεῖ May 27 '23 at 12:29
  • @dalfaB - Oddly, I'm getting the opposite result from `gcc`. It chokes on the `int s` when compiling `temp.c`, not when compiling the exact same code as `temp.cpp`... – T.J. Crowder May 27 '23 at 12:31
  • *"These online compilers are claiming to use gcc"* Citation needed. For instance, for the first one, there's nothing saying it uses `gcc` in [About](https://www.onlinegdb.com/about) or [FAQ](https://www.onlinegdb.com/faq). – T.J. Crowder May 27 '23 at 12:32
  • 1
    gcc works for me but clang does not. Perhaps the parts I had to invent don't match what you're using. https://godbolt.org/z/87dv6a5Ks – Retired Ninja May 27 '23 at 12:32
  • 1
    @dalfaB: The braces are needed in the base language of the current C standard because, in its grammar, declarations are not statements, and only statements can have labels. So `case 2: int s;` fails when compiling with strict options because there is no labeled declaration in the grammar, but `case 2: { int s; … }` would work because a compound statement (`{ … }`) can contain declarations and is a statement, so it can be labeled with a `case`. – Eric Postpischil May 27 '23 at 12:38

1 Answers1

4

Testing with Compiler Explorer suggests GCC changed in version 11 to accept labeled declarations, which is an extension to the current C standard language.

In the OnlineGDB compiler you link to, adding the extra switch -pedantic produces a warning message, confirming that the compiler is treating this as an extension to the language but knows it is not part of the C standard.

GCC documents this: “ISO C2X allows labels to be placed before declarations and at the end of a compound statement. As an extension, GNU C also allows all this in C90 mode.” (“ISO C2X” means the upcoming ISO C standard, likely to be C 2023.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312