0

Possible Duplicate:
Practical use of extra braces in C
Unnecessary curly braces in C++?

What is the usage of the braces, for example as shown below:

int var;
{
  some coding...
  ...
}

there is no function name before the braces, nor typedef, etc.

Updated: i found this code in gwan sqlite.c example, http://gwan.com/source/sqlite.c
i partially quote it below:

...some coding
sqlite3_busy_timeout(db, 2 * 1000); // limit the joy

   // -------------------------------------------------------------------------
   // create the db schema and add records
   // -------------------------------------------------------------------------
   {   //<-- here is the starting brace
      static char *TableDef[]=
      {
         "CREATE TABLE toons (id        int primary key,"
                             "stamp     int default current_timestamp,"
                             "rate      int,"
                             "name      text not null collate nocase unique,"
                             "photo     blob);",
         // you can add other SQL statements here, to add tables or records
         NULL
      };
      sqlite3_exec(db, "BEGIN EXCLUSIVE", 0, 0, 0);
      int i = 0;
      do
      { 
         if(sql_Exec(argv, db, TableDef[i])) 
         {
            sqlite3_close(db);
            return 503;
         }
      }
      while(TableDef[++i]);

      // add some records to the newly created table
      sql_Exec(argv, db, 
               "INSERT INTO toons(rate,name) VALUES(4,'Tom'); "
               "INSERT INTO toons(rate,name) VALUES(2,'Jerry'); "
               "INSERT INTO toons(rate,name) VALUES(6,'Bugs Bunny'); "
               "INSERT INTO toons(rate,name) VALUES(4,'Elmer Fudd'); "
               "INSERT INTO toons(rate,name) VALUES(5,'Road Runner'); "
               "INSERT INTO toons(rate,name) VALUES(9,'Coyote');");

      sqlite3_exec(db, "COMMIT", 0, 0, 0);

      // not really useful, just to illustrate how to use it
      xbuf_cat(reply, "<br><h2>SELECT COUNT(*) FROM toons (HTML Format):</h2>");
      sql_Query(argv, db, reply, &fmt_html, "SELECT COUNT(*) FROM toons;", 0);
   } //<-- here is the ending brace  
...some coding
Community
  • 1
  • 1
k.k. lou
  • 1,805
  • 2
  • 13
  • 16
  • 1
    @unwind: I wouldn't close this as a duplicate of a C++ question, since it has different uses. In C++ this can be used for RAII over a certain block, while in C89 it can be used to declare new local variables. – interjay Dec 13 '12 at 11:56
  • 1
    @GrijeshChauhan: Desctructors are called when the variable goes out of scope. So adding a block at a specific place can allow you to use [RAII](http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization) over a part of a function. For example, you can use this to automatically unlock a mutex or free a resource when leaving the block. – interjay Dec 13 '12 at 12:01
  • @interjay: you should write in answer. Its very good. – Grijesh Chauhan Dec 13 '12 at 12:10
  • @interjay Yeah, I realize that ... My answer to that question does mention C though, so I felt it was useful. Too late to edit. – unwind Dec 13 '12 at 12:14

3 Answers3

3

Statements can be grouped into blocks, and the braces indicate the start and end of a block. A function body is a block. Blocks introduce a new variable scope that starts at the opening brace and ends at the closing brace.

What you've got there is a block.

2

braces usage without function name

I guess that rather than what it is the answer can focus on why this is done.

For example, you can re-use a variable name to do something else, using a different type (the SQLite example does this to keep using the same terminology while restarting from scratch instead of risking naming conflicts):

{
   int i = 2; 
   ...
   {
      int i = 10; // this is a different variable

      // the old value of 'i' will be restored once this block is exited.
   }
}
{
   void *i = alloca(16 * 1024); // this memory will be freed automatically
   ...                          // when the block will be exited
}

But this also lets you free memory allocated on the stack with alloca(), like done above.

This is also a clear indication for the compiler that the variables defined in a block are no longer needed (this can be useful to make sure that CPU registers are freed for other tasks).

As you see, defining a scope can have cosmetic and technical uses. Both of which are useful.

Gil
  • 3,279
  • 1
  • 15
  • 25
1

To create a new scope for local variables within {}

for example in C:

fun(){ 
  int i;   // i -1
  {
    int i;   // i -2 its new variable 
  }

}
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208