85
int a = 10;
switch(a){
case 0:
    printf("case 0");
    break;
case 1:
    printf("case 1");
    break;
}

Is the above code valid?

If I am sure that int a will not have any other value than 1 and 0, can I avoid default?

What if in any case a value will be different from 1 and 0?

I know this is a silly question but I was thinking that perhaps it would be illegal or undefined behavior soI just asked to make sure.

4
  • 9
    i have tried but i was thinking that perhaps it would be illegal or undefined behavior so just asked to make sure Commented Nov 5, 2011 at 16:04
  • In such cases I always put a default in and throw an exception. Now in C#7 with the greater flexibility of "case when" I've moving to covering all possible cases and using default to cover the impossible ones. Douglas Adams would understand that perspective. Commented May 21, 2017 at 11:37
  • I would generally add ASSERT(false); so that I would detect unexpected values in DEBUG builds.
    – Phil1970
    Commented Mar 29, 2018 at 16:31
  • Read learn.microsoft.com/en-us/dotnet/csharp/language-reference/…
    – RJN
    Commented Jun 27, 2018 at 9:35

7 Answers 7

136

The code is valid. If there is no default: label and none of the case labels match the "switched" value, then none of the controlled compound statement will be executed. Execution will continue from the end of the switch statement.

ISO/IEC 9899:1999, section 6.8.4.2:

[...] If no converted case constant expression matches and there is no default label, no part of the switch body is executed.

50

As others have pointed out it is perfectly valid code. However, from a coding style perspective I prefer adding an empty default statement with a comment to make clear that I didn't unintentionally forget about it.

int a=10;
switch(a)
{
case 0: printf("case 0");
         break;
case 1: printf("case 1");
         break;
default: // do nothing;
         break;
}

The code generated with / without the default should be identical.

6
  • 15
    If you intend to handle all the cases in an enum, I would recommend omitting the default case, because most compilers will warn if an enum case is forgotten in a switch statement.
    – rdb
    Commented Jul 18, 2016 at 12:35
  • 2
    It would be even better to have an assert (or similar) if you expect it to never fall through. A lot of error can be caught this way. Commented Mar 6, 2021 at 1:57
  • @rdb Most "compilers"? Isn't it the IDE that does this?
    – Kathir
    Commented May 14, 2021 at 10:39
  • @Kathir I wouldn't know, I don't use an IDE. These warnings are generally generated by compilers, but it is possible for the IDE to relay them to the developer. It is conceivable that some IDEs have their own code-checkers that warn the developer of this as well.
    – rdb
    Commented May 15, 2021 at 9:33
  • @rdb Okay. Both are possible. The compiler generating a warning during compilation. The IDE does something called linting. I do not know the internals of this linting process. BUT, I don't think it compiles the code
    – Kathir
    Commented May 15, 2021 at 10:59
9

It is perfectly legal code. If a is neither 0 or 1, then the switch block will be entirely skipped.

5

It's valid not to have a default case.

However, even if you are sure that you will not have any value rather than 1 and 0, it's a good practice to have a default case, to catch any other value (although it is theoretically impossible, it may appear in some circumstances, like buffer overflow) and print an error.

5
  • You are assuming that it is an error not to match one of the case statements - probably true in this case. You may want to only perform an action some possible values of a variable and to just carry on in other cases. IMHO, this is a perfectly reasonable use of a switch statement.
    – CB Bailey
    Commented Nov 5, 2011 at 16:06
  • @Charles: He wrote in the question: see if i am very much sure that int a will not have any value rather than 1 and 0 then in that case can i avoid default:
    – Igor
    Commented Nov 5, 2011 at 16:07
  • 2
    I wasn't disagreeing with you, I'm sorry if it came across like that. I was just pointing out that although, in general, it is good practice to have a default label, it's not necessarily bad practice to omit one.
    – CB Bailey
    Commented Nov 5, 2011 at 16:09
  • @CharlesBailey It seems very much worthwhile to include the default case if for no other reason than documenting that you at least considered the default case (even if there is no executable code therein). Given that there is relatively high benefit to including it and no real advantage from omitting it, I would say it's very good practice to include it.
    – weberc2
    Commented Nov 20, 2012 at 19:01
  • 4
    @weberc2 there is an advantage gained from omitting it: if you are switching on an enum, some compilers will warn if an enum value is missed in the switch case. This can be a very helpful tool to find places where a case should be added after a value is added to an enum.
    – rdb
    Commented Jul 18, 2016 at 12:36
3

Yes, the above code is valid.

If the switch condition doesn't match any condition of the case and a default is not present, the program execution goes ahead, exiting from the switch without doing anything.

3

Default is not mandatory, but it always good to have it.

The code is ideally, but our life is not, and there isn't any harm in putting in a protection there. It will also help you debugging if any unexpected thing happens.

1
  • The harm is, the code needs to test for a default case before jumping to a case label. But I dont know if compilers do the test nonetheless. Commented Feb 16 at 3:05
1

It's same like no if condition is matched and else is not provided.

default is not an mandatory in switch case. If no cases are matched and default is not provided, just nothing will be executed.

Not the answer you're looking for? Browse other questions tagged or ask your own question.