Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why enclose blocks of C code in curly braces?

I am looking at some C code, and have noticed it is full of these curly braces surrounding blocks of code without any sort of control structure. Take a look-see:

//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 blocks like this in the code? It is chock full of 'em. Is there some kind of performance benefit? Some mystical C thing? Why???

edit: This code if from BWA, a bioinformatics program that aligns small sequences to large reference ones using the Burrows-Wheeler transform, in case any of you were wondering. This code example isn't particularly relevant to the functionality of the application.

like image 570
jergason Avatar asked Nov 05 '09 01:11

jergason


People also ask

Why do we use block of statements with braces in C?

Braces improve the uniformity and readability of code. More important, when inserting an additional statement into a body containing only a single statement, it is easy to forget to add braces because the indentation gives strong (but misleading) guidance to the structure.

What is enclosed in curly braces?

Curly brackets are commonly used in programming languages such as C, Java, Perl, and PHP to enclose groups of statements or blocks of code. Below is an example of what an enclosed statement looks like in Perl. } until ($value >= 100); In the example above are two enclosed statements using curly brackets.

Why we use curly bracket in C programming?

Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.

Can the curly brackets be used to enclose a single line of code in C?

90) Can the curly brackets { } be used to enclose a single line of code? While curly brackets are mainly used to group several lines of codes, it will still work without error if you used it for a single line.


2 Answers

Legacy code needed { } in order to do declarations at all

In C89, you couldn't just do int i; anywhere; declarations were only valid at the beginning of blocks.

So:

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

...wasn't valid, but

a = 1 if (e) {   int i; 

...was fine, as was a plain block.

The resulting style continued even after declarations became valid (C99) block-item(s), partly by inertia, partly for backwards portability, and also because it makes sense to establish a scope for new declarations.

like image 194
DigitalRoss Avatar answered Sep 21 '22 05:09

DigitalRoss


To scope variables. E.g. the variable tmp_argv will only be valid between the braces.

like image 26
jldupont Avatar answered Sep 21 '22 05:09

jldupont