6

I've come across a bit of code that contains a couple code blocks, delineated with curly braces {}. There is no line before the code blocks marking them as part of if statements, function definitions, or anything else. Just a code block floating in the middle of a function. Is there any meaning to this? gcc seems perfectly happy going through the code; I can only imagine it is some way to allow the original coder to split up blocks of functionality visually...

user17925
  • 989
  • 2
  • 10
  • 20
  • http://stackoverflow.com/questions/1677778/why-enclose-blocks-of-c-code-in-curly-braces @Goathens, i think question asked in above URL will answer your query. – Anil Vishnoi Sep 03 '10 at 14:16
  • @Anil Yes it does. I promise I did try to find a similar question before posting! – user17925 Sep 03 '10 at 14:44

5 Answers5

11

It creates a scope. Are there automatic variables defined inside the blocks? If so, then the scope of those variables is confined to the block. It's useful for temporary variables that you don't want polluting the rest of the function, and it's also useful when writing C89, where variable definitions must be at the start of a block.

So, instead of:

int main() {
    int a = 0;
    int b;
    int i;

    for (i = 1; i < 10; ++i) {
        a += i;
    }

    b = a * a;
    // do something with a and b
}

You could have:

int main() {
    int a = 0;
    {
        int i;
        for (i = 1; i < 10; ++i) {
            a += i;
        }
    }

    {
        int b = a * a;
        // do something with a and b
    }
}

Obviously if you're doing this, you also have to ask yourself if the blocks wouldn't be better off as separate functions.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • +1 for examples and some discussion of *why* someone would use scoping. – Edmund Sep 03 '10 at 14:25
  • When you say 'polluting the rest of the function', are you referring to readability/maintainability? Or do you mean that this technique can help conserve memory? Or something else? – Andy J Dec 12 '14 at 03:30
  • @AndyJ0076: readability/maintainability. It could conserve memory, that's up to the compiler, but that's not what I meant because it doesn't reliably do so. IIRC compilers typically lay down one stack frame for the whole function, regardless of the scopes of the variables in it. If they re-use stack slots it's at least as likely to be due to the compiler's own liveness analysis as due to scoping. – Steve Jessop Dec 12 '14 at 09:41
9

Standalone curly braces are used for scoping—any variables declared in a block aren't visible outside it.

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
2

If the code blocks contain local variable declarations (your description is not clear about what's inside), they may be there to limit the scope of those variables. I.e.

int i = ...;
// i is visible here
{
  int j = ...;
  // both i and j are visible here
}
// j went out of scope, only i is visible here
Péter Török
  • 114,404
  • 31
  • 268
  • 329
2

It is used to create a scope. A scope if useful to declare variables that can only be used inside this scope. For example, if a variable is declared before the braces, it can be used inside the braces of after them. At the contrary, if a variable is declared inside the braces, it can only be used inside the braces. Hope this helps.

Live
  • 1,981
  • 16
  • 17
0

It usually means it was written to declare a variable part way through a larger function, but the variable only needs a very limited scope, or may even need to hide something. It is entirely legimitate - it simply introduces a block. The big issue is always "what is the correct indentation for that code".

It can also be used by pre-processors that convert some other language into generated C. The converted material is often wrapped in a block (and possibly #line directives too).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278