C语言 C Switch-case 花括号在每个 case 之后
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4241545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C Switch-case curly braces after every case
提问by Ben
In a C switch-caseflow control, it's required to put curly braces { }after a caseif variables are being defined in that block.
在 Cswitch-case流控制中,需要在该块中定义的if 变量之后放置花括号{ }case。
Is it bad practice to put curly braces after every case, regardless of variable declaration?
case无论变量声明如何,在 each 之后放置大括号是不好的做法吗?
For example:
例如:
switch(i) {
case 1: {
int j = 4;
...code...
} break;
case 2: { //No variable being declared! Brace OK?
...code...
} break;
}
采纳答案by bta
It's certainly not invalid to use braces in every case block, and it's not necessarily bad style either. If you have some case blocks with braces due to variable declarations, adding braces to the others can make the coding style more consistent.
在每个 case 块中使用大括号当然不是无效的,也不一定是不好的风格。如果由于变量声明,您有一些带有大括号的 case 块,在其他情况下添加大括号可以使编码风格更加一致。
That being said, it's probably not a good idea to declare variables inside case blocks in straight C. While that might be allowed by your compiler, there's probably a cleaner solution. Mutually-exclusive case blocks may be able to share several common temporary variables, or you may find that your case blocks would work better as helper functions.
话虽如此,在直接 C 中声明 case 块内的变量可能不是一个好主意。虽然您的编译器可能允许这样做,但可能有一个更简洁的解决方案。互斥的 case 块可能能够共享几个常见的临时变量,或者您可能会发现您的 case 块作为辅助函数会更好地工作。
回答by SuperDuck
Braces may be used in every case statement without any speed penalty, due to the way compilers optimize code. So it's just the style and the preference of the coder.
由于编译器优化代码的方式,可以在每个 case 语句中使用大括号而不会造成任何速度损失。所以这只是编码器的风格和偏好。
The most preferred usage is not using braces, though the usage of them in every case during an active development may be found easier to make some additions on the code every now and then.
It's just the easthetics; because a 'case' statement doesn't need only a single command, but will walk through the code as it works as a label. So blocks are not needed, and are not invalid.
In 'case's with variables; braces are used just-in-case, to create contexts for variables, and it makes big sense to use them. Some compilers on different platforms show different behaviours if they are not included.
最优选的用法是不使用大括号,尽管在活跃的开发过程中在每种情况下使用它们可能会更容易不时地对代码进行一些添加。
这只是美学;因为“case”语句不仅需要单个命令,还可以遍历代码,因为它用作标签。所以块是不需要的,也不是无效的。
在带有变量的情况下;大括号用于以防万一,为变量创建上下文,使用它们很有意义。如果不包括在内,不同平台上的某些编译器会显示不同的行为。
回答by Jens Gustedt
Generally it is bad practice jump over the initialization of a variable, be it with gotoor switch. This is what happens when you don't have the the blocks per case.
通常,跳过变量的初始化是不好的做法,无论是使用goto还是switch。这就是当您没有 per 块时会发生的情况case。
There is even a case in C99 where jumping over the initialization is illegal, namely variable length arrays. They must be "constructed" similarly as non-PODs in C++, their initialization is necessary for the access of the variable later. So in this case you mustuse the block statement.
在 C99 中甚至存在跳过初始化是非法的情况,即变长数组。它们必须像 C++ 中的非 POD 一样“构造”,它们的初始化对于稍后访问变量是必要的。所以在这种情况下你必须使用块语句。
回答by R.. GitHub STOP HELPING ICE
I consider it bad style to use braces in each case. Cases are labels in C, akin to gotolabels. And in the current C language, you're free to declare variables in each case(or anywhere you like) without introducing new blocks, though some people (myself included) also consider that bad style.
我认为在每个case. 案例是 C 中的标签,类似于goto标签。在当前的 C 语言中,您可以在每个case(或您喜欢的任何地方)自由地声明变量而无需引入新块,尽管有些人(包括我自己)也认为这种风格很糟糕。
回答by Steve Barnes
Just to add a minor point many editors & IDEs allow blocks to be collapsed and/or auto indented and several allow you to jump to the matching brace - I personally don't know of any that allow you to jump from a break to the matching case statement.
只是为了添加一个小点,许多编辑器和 IDE 允许块折叠和/或自动缩进,并且有几个允许您跳转到匹配的括号 - 我个人不知道任何允许您从中断跳转到匹配的案例陈述。
When debugging, or re-factoring, other peoples, (or even your own after a few months), code that contains complex case statements the ability to both collapse sections of the code and to jump to matching cases is invaluable, especially if the code contains indentation variations.
在调试或重构其他人(甚至几个月后您自己的人)时,包含复杂 case 语句的代码具有折叠代码部分和跳转到匹配用例的能力是无价的,尤其是如果代码包含缩进变化。
That said it is almost always good advice to avoid complex case statements like the plague.
也就是说,避免像瘟疫这样的复杂案例陈述几乎总是好的建议。

