Why enclose C code blocks in curly braces?

I look at some C code and notice that it is filled with these curly braces surrounding the blocks of code without any control structure. Take a look:

//do some stuff . . . fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); { //a block! why not? char *tmp_argv[3]; tmp_argv[0] = argv[0]; tmp_argv[1] = str; tmp_argv[2] = prefix; t = clock(); fprintf(stderr, "[bwa_index] Convert nucleotide PAC to color PAC... "); bwa_pac2cspac(3, tmp_argv); fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); } 

Why would you insert such blocks into your code? It is filled with them. Is there any kind of performance? Something mystical C? Why???

edit: This code, if from BWA , is a bioinformatics program that aligns small sequences with large references using the Burrows-Wheeler transform , if you're interested. This sample code is not particularly relevant to the functionality of the application.

+54
c scope curly-braces
Nov 05 '09 at 1:25
source share
9 answers

Necessary code for obsolete code {} to make ads in general

In C89, you could not just do int i; anywhere; Ads were only valid at the beginning of the blocks.

So:

 a = 1; int i; /* error */ i = 2; 

... invalid but

 a = 1 if (e) { int i; 

... it was beautiful, like a regular block.

The resulting style continued even after declarations became valid (C99) block elements, partly by inertia, partly for reverse portability, and also because it makes sense to set the scope for new announcements.

+82
Nov 05 '09 at 1:31
source share

Variable areas. For example. tmp_argv will only be valid between braces.

+37
Nov 05 '09 at 1:28
source share

A block is an area that determines the lifetime of variables, as well as their visibility to the compiler. Thus, the variables that are created inside the block go away when control exits the block.

This can be very convenient when these variables are instances of classes with constructors and destructors.

However, in your example there are not many advantages.

+7
Nov 05 '09 at 1:30
source share

It creates a scope. Stack objects are destroyed when they go beyond. It seems that he is doing some typing, which would mean that every block is what they wanted. However, I do not see any objects with a timer, so yes, it does not make sense.

+6
Nov 05 '09 at 1:28
source share

The variables that you declare inside the block are local to this block. This way you can override tmp_argv elsewhere in your code (below) without contradicting this part of the code.

+5
Nov 05 '09 at 1:28
source share

Another use case I recently discovered is when you have open / closed semantics, and you want to clearly indicate the internal code:

 f = fopen('file'); { // do stuff } fclose(f); 

This works well to remind you to close / release objects and make the code a bit cleaner.

+5
Nov 05 '09 at 13:41
source share

It's all? Maybe the programmer uses tmp_argv somewhere else in the code. I can’t think of any other reason, since the tmp_argv between { and } is separate from any external shapes.

+1
Nov 05 '09 at 1:30
source share

I sometimes use blocks in these cases: - To localize variables - Or easier to read ...

+1
Nov 05 '09 at 3:59
source share

Hmm - I may not be on the map here, but I think that a local variable defined inside such a block will not be valid outside the block

-2
Nov 05 '09 at 1:28
source share



All Articles