实际使用用C额外的括号 [英] Practical use of extra braces in C
问题描述
我知道 {}
用于独立的实体,如函数,类和条件分支,但什么其他用途,他们将在这里?
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;
}
编辑:
我发现,它可能是主要用于信息隐藏有用的,与嵌套函数的一起。从它下面的答案似乎是,他们可以在调试过程中被用作标记,在版本中删除,但是,这不应该被认可。
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.
推荐答案
括在括号中的code {
}
创建一个 <一个href=\"http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/zexscope_c.htm\"相对=nofollow>范围 。结果
创建本地范围可以有像一些原因:
Enclosing a code in braces {
}
creates an Scope.
Creating an local scope can have number of reasons like:
- 允许您在封闭范围内重复使用的变量名。
- 在函数中间定义变量。结果
除了创建一个在范围内的任何地方开始变量C89是不允许的,但由于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;
}
在您的例子code,结果
额外的 {
&安培; }
没有任何作用,他们只是多余的code。
In your example code,
the extra {
& }
do not serve any purpose, they are just redundant code.
作为@马丁建议在评论,因为在封闭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.
不过,就个人而言,我会preFER添加适当的注释与关键字code这将搜索显示出来,而不是添加这些多余的code。
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.
这篇关于实际使用用C额外的括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!