59

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.

Is it bad practice to put curly braces after every case, regardless of variable declaration?

For example:

switch(i) {
  case 1: {
    int j = 4;
    ...code...
  } break;

  case 2: {  //No variable being declared! Brace OK?
    ...code...
  } break;
}
Ben
  • 54,723
  • 49
  • 178
  • 224

5 Answers5

55

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.

bta
  • 43,959
  • 6
  • 69
  • 99
  • 8
    Just a note - one can declare variables at the beginning of switch before the first case (and thus share these variables without adding more curly braces. – martinkunev Jan 14 '16 at 16:04
19

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.

DarkWingDuck
  • 2,028
  • 1
  • 22
  • 30
  • Do you know if there's a speed penalty, or just a loss in style points? – Ben Nov 22 '10 at 01:56
  • There is no speed penalty, and it compiles like you don't have them. I'm editing the answer to include this. – DarkWingDuck Nov 22 '10 at 09:33
  • Thanks Duck, unfortunately @bta's solution is a bit clearer in my muddled head, but +1 for your help. – Ben Nov 23 '10 at 05:26
  • It is not true that "braces are used just-in-case, to create contexts for variables," - it's mostly because of the C99 syntax limitations (see next answer or https://stackoverflow.com/a/8049940/). – kiler129 Sep 17 '21 at 16:40
5

Generally it is bad practice jump over the initialization of a variable, be it with goto or switch. This is what happens when you don't have the the blocks 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 must use the block statement.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
4

I consider it bad style to use braces in each case. Cases are labels in C, akin to goto labels. 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.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 8
    but if you don't delimit a block, any variable you declare in a `case` will be available in the next one. if you have any big `switch`, it could create some nasty bugs; better either delimit the blocks, or reduce them to little more than function calls. – Javier Nov 22 '10 at 01:50
  • @R and anyone else - Ahh - so the actual practice of defining variables in blocks could be bad? – Ben Nov 22 '10 at 01:54
  • @Javier - Some of my compilers crash without the braces; the "available in the next one" apparently doesn't happen... – Ben Nov 22 '10 at 01:55
  • 9
    If your switch block is so big that it needs case-local variables to keep it organized, perhaps it's time to factor your code into functions... – R.. GitHub STOP HELPING ICE Nov 22 '10 at 03:13
  • @R - well put. Thanks for the help too. – Ben Nov 23 '10 at 05:18
1

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.

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.

That said it is almost always good advice to avoid complex case statements like the plague.

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73