C Switch-case curly braces after every case

前端 未结 5 1855
感动是毒
感动是毒 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:49

    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.

    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.

提交回复
热议问题