为什么在大括号括C $ C $的C块? [英] Why enclose blocks of C code in curly braces?
问题描述
我在看一些C code,并已注意到它充满了周围的code块没有任何类型的控制结构的这些花括号。看看看的:
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);
}
为什么你会插入这样的块中的code?这是座充满了他们。是否有某种性能优势?有些神秘的东西Ç?为什么???
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???
编辑::此code。如果从 BWA ,即对齐小序列的生物信息学计划使用 Burrows-Wheeler变换,在任何情况下,你想知道。此$ C $℃实施例不是本应用程序的功能特别相关。
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.
推荐答案
在C89,你不能只是做 INT I;
的任何地方;声明是只在代码块的开始有效。
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.
所以:
a = 1;
int i; /* error */
i = 2;
...不是有效的,但
...wasn't valid, but
a = 1
if (e) {
int i;
......很好,因为是一个简单的块。
...was fine, as was a plain block.
由此产生的风格继续即使后声明成为有效的(C99)的块项目的(S),部分原因是惯性,部分是为了向后便携性,还因为它是有道理的,建立新的作用域声明。
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.
这篇关于为什么在大括号括C $ C $的C块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!