对于生成的代码,我可以选择缩进或不缩进大括号,仅用于容纳范围内的变量.目前它没有在这个级别缩进,我想知道我是否要通过缩进来暗示嵌套结构?常见的做法是什么?
/* loop through the total number of letter a rules */
for (a = 0; a < (number_a_rules - 1); a++)
{
/* loop through secondary position rules */
{
int a2end = arulestableend[2];
for (int a2 = arulestablestart[2]; a2 < a2end; a2++)
{
/* stuff */
}
}
} /* end for a 0 to numberarules -1 */
Run Code Online (Sandbox Code Playgroud)
与
/* loop through the total number of letter a rules */
for (a = 0; a < (number_a_rules - 1); a++)
{
/* loop through secondary position rules */
{
int a2end = arulestableend[2];
for (int a2 = arulestablestart[2]; a2 < a2end; a2++)
{
/* stuff */
}
}
} /* end for a 0 to numberarules -1 */
Run Code Online (Sandbox Code Playgroud)
澄清:使用调试器额外的缩进意味着难以读取代码的另一级循环...
对我而言,没有缩进就更具误导性.我看一下这个没有缩进的版本并想"这里有什么不对"?
是否真的有必要使用多余的支架?
我想回答ojblass的评论,它会占用更多空间,而不是我认为的评论.
我认为你不能在没有大括号的情况下在c中声明变量...至少在一些悲惨的编译器上.
你在C中不像C++那样自由; 你要做的是在任何可执行代码之前放入任何新的声明.所以,在这个片段中
for (a = 0; a < (number_a_rules - 1); a++)
{
/* loop through secondary position rules */
{
int a2end = arulestableend[2];
for (int a2 = arulestablestart[2]; a2 < a2end; a2++)
Run Code Online (Sandbox Code Playgroud)
你可以写出来
for (a = 0; a < (number_a_rules - 1); a++)
{
int a2end = arulestableend[2];
for (int a2 = arulestablestart[2]; a2 < a2end; a2++)
Run Code Online (Sandbox Code Playgroud)
它会很好地工作.另一方面,for循环中的声明也不是直C.
现在,什么是可能的,而且在本例中不会显示,如果是发问者是使用大括号来限制范围,所以他有一个简单的命名空间管理; 就是这样,你可以拥有
{
int a2 = some_good_thing();
// do stuff
}
{
int a2 = something_else(); // Now a different a2,
// and it's the compiler's problem
// do other stuff
}
Run Code Online (Sandbox Code Playgroud)
(注意:对不起,OJ,没有意识到你是提问者.我的眼睛受伤了,请对语法进行必要的更正.)
| 归档时间: |
|
| 查看次数: |
437 次 |
| 最近记录: |