1

I know {} are used to separate entities such as functions, classes and conditional branching, but what other use would they have here?

#import <stdio.h>

int main(void) {
    {{{
        printf("main\n");
    }}}
    return 0;
}

EDIT:

I found that it may be useful primarily for information hiding, along with nested functions. From the answers below it would seem they can be used as a marker during debugging and be removed in the release, but that this should not be endorsed.

Aram Kocharyan
  • 20,165
  • 11
  • 81
  • 96
  • 1
    To define new local variable in middle of function in C89. – osgx Jan 27 '12 at 03:50
  • But without a return from the `{}` what use would this have? – Aram Kocharyan Jan 27 '12 at 03:51
  • *Nested Functions* are not allowed in C as per the C standard.You cannot defined a function local to another function in C.You can however declare a function inside another function.GCC provides a [compiler extension](http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html) to support nested functions but using it renders your code non-portable across other compilers. – Alok Save Jan 27 '12 at 04:49

4 Answers4

6

Enclosing a code in braces { } creates an Scope.
Creating an local scope can have number of reasons like:

  • Allows you to reuse a variable name in enclosing scope.
  • Define variables in middle of function.
    Creating variables anywhere except at the start of an scope was not allowed in c89, but it is allowed since c99.

Online Example Code Sample:

#include<stdio.h>

int main()
{
    int i = 10;
    {
         int i = 5;
         printf("i is [%d]\n",i);
    }
    printf("i is [%d]\n",i);

    return 0; 
}

In your example code,
the extra { & } do not serve any purpose, they are just redundant code.

As @Martin suggests in comments, since enclosing code in {{{ & }}} is just similar to { & }, it might be used as an tag/pattern for easy search.

However, Personally, I would prefer adding appropriate comment to the code with an keyword which would show up in search rather than add such redundant code.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • So I suppose you could use it to separate a set of processes in a single function into groups – Aram Kocharyan Jan 27 '12 at 03:53
  • 1
    Probably a marker for a search and replace of "{{{" with "/*" in release – Martin Beckett Jan 27 '12 at 03:54
  • 1
    @AramKocharyan: Caution: The word *Process* has different meanings especially when you are speaking in context of c.To answer your comment, Yes,You can use `{``}` to logically group *sections* of your function in to different scopes. – Alok Save Jan 27 '12 at 04:02
2

That syntax (three curly braces in a row) doesn't mean anything special in standard C. The author of such code might use it to indicate something about the code inside, like that it's just there for debugging.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
2

you can introduce a new scope, which then allows you to introduce new variables.... which can be useful in C89. Not too normal though, but occasionally useful.

{
  int x =2 ;
  printf("%d", x);
  {
    int y = 7;
    printf("%d", y);
  }
}
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
0

Extra braces gives you scope as Als mentioned. This can be used in case of Smart Pointers effectively. e.g., consider the following code in C++ (MSVC compiler)

int i = 0;
i++;
//More code follows 
...

{
    CComBSTR bstr("Hello");
    //use this bstr in some code
    .....
}

After the end braces, bstr won't be available. Furthermore, since it has gone out of scope, therefore, the destructor will also get called automatically.

Aamir
  • 14,882
  • 6
  • 45
  • 69
  • So scope can be used to ensure something executes but is removed from the local scope as the stack frame returns to the original function? – Aram Kocharyan Jan 27 '12 at 03:56