1

Is creating local scopes with just a pair of brackets, would that be considered standard C?

#include <stdio.h>

int main(int argc, char *argv[]) {
    {
        register char x = 'a';
        putchar(x); // works
    }
    //putchar(x); wont work
}

Or is it best to not use this? Is it a GCC compiler extension?

I have once been told that the accepted practice is a do {...} while (0); loop. Is it true that all C compilers will recognise this practice just like it is safe to assume any given C compiler will recognise an if statement?

I tried googling this, and my results were about scope behavior, and had nothing to do with manually applying scopes.

  • 9
    Yes, it is standard. And has always been standard. – wildplasser Feb 05 '20 at 22:58
  • 2
    Many people consider *not* creating a scope after `if` or `while` to be bad practice. – Artyer Feb 05 '20 at 23:01
  • 3
    Yes, but honestly, you should really have made the effort to consult a C language book before asking such a basic question. – einpoklum Feb 05 '20 at 23:01
  • You should not see the `do … while (0)` thing in vanilla code, just in `#define` directives. [It is a kludge for macros.](https://stackoverflow.com/questions/154136/why-use-apparently-meaningless-do-while-and-if-else-statements-in-macros) – Eric Postpischil Feb 06 '20 at 00:19
  • Yes, It's even K&R standard. If you look even in the first edition of "The C programming language" by Brian Kernighan and Denis Ritchie, you'll find that a pair of brackets means a block, in which you can start declaring new objects as you do at the beginning of `main()`. – Luis Colorado Feb 06 '20 at 07:15

3 Answers3

5

Yes, it is standard; this is creation of block scope, as supported by C language.

Regarding "best to use" part, it depends, certainly it might be a bit confusing to some people.

This might come in very useful with generated code though, so you don't need to bother (as much) with unique identifiers for local vars:

int main(int argc, char *argv[]) {
  {
    int x = 4;
    printf("%d\n", x);
  }
  {
    int x = 5;
    printf("%d\n", x);
  }
}
Adam Kotwasinski
  • 4,377
  • 3
  • 17
  • 40
2

Yes, it is standard; you can always introduce a new scope with an extra pair of {}

I often use it in conditionally compiled code, to avoid unused variable warnings:


#if DEBUG
    {
    struct node *tmp;
    for (tmp=this; tmp; tmp= tmp->next) {
        printf(stderr, "Node %u: %s\n", tmp->num, tmp->name);
        }
     }
#endif
wildplasser
  • 43,142
  • 8
  • 66
  • 109
  • In macros do{ "your code"} while (0) is often used to force the semicolon after the marco (Make sure that the marco behavious like a function). I know this is not part of the question but I thought its worth to mention it. – Schafwolle Feb 05 '20 at 23:26
  • 1
    I know the *funky* `do {...} while(0)` macro, but that was not part of the question (and will only cause confusion, IMHO) – wildplasser Feb 05 '20 at 23:31
  • How about `for (struct node *tmp=this; tmp; tmp= tmp->next) { /* ... */ }`? – user16217248 Feb 22 '23 at 03:50
2

This is standard C.

Section 6.8p1 of the C standard gives the following syntax for a statement:

statement:
  labeled-statement
  compound-statement
  expression-statement
  selection-statement
  iteration-statement
  jump-statement

Where a compound-statement is defined in section 6.8.2p1:

compound-statement:
   { block-item-list(opt) }

block-item-list:
  block-item
  block-item-list block-item

block-item:
  declaration
  statement

What this says is that anyplace that a statement can occur, a compound statement (i.e. a set of statements surrounded by {}) can occur.

dbush
  • 205,898
  • 23
  • 218
  • 273