Ben
Ben

Reputation: 57267

C Switch-case curly braces after every case

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;
}

Upvotes: 60

Views: 81286

Answers (5)

Steve Barnes
Steve Barnes

Reputation: 28405

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.

Upvotes: 1

bta
bta

Reputation: 45057

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.

Upvotes: 56

DarkWingDuck
DarkWingDuck

Reputation: 2128

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.

Upvotes: 20

Jens Gustedt
Jens Gustedt

Reputation: 78923

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.

Upvotes: 7

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215287

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.

Upvotes: 5

Related Questions