C Switch-case curly braces after every case

前端 未结 5 1840
感动是毒
感动是毒 2020-12-08 02:07

In a C switch-case flow control, it\'s required to put curly braces { } after a case if variables are being defined in that block.

5条回答
  •  抹茶落季
    2020-12-08 02:52

    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.

    • 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.

提交回复
热议问题