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

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
  • 8
    i have tried but i was thinking that perhaps it would be illegal or undefined behavior so just asked to make sure – Jeegar Patel Nov 05 '11 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. – Paulustrious May 21 '17 at 11:37
  • I would generally add `ASSERT(false);` so that I would detect unexpected values in DEBUG builds. – Phil1970 Mar 29 '18 at 16:31
  • Read https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch – RJN Jun 27 '18 at 09:35

8 Answers8

134

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.

jww
  • 97,681
  • 90
  • 411
  • 885
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
48

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.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • 14
    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 Jul 18 '16 at 12:35
  • 1
    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. – Peter Mortensen Mar 06 '21 at 01:57
  • @rdb Most "compilers"? Isn't it the IDE that does this? – Kathir May 14 '21 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 May 15 '21 at 09:33
  • @rdb Okay. Both are possible. The compiler generating a warning during compilation. The IDE does something called [linting](https://stackoverflow.com/questions/8503559/). I do not know the internals of this linting process. BUT, I don't think it compiles the code – Kathir May 15 '21 at 10:59
  • It may be nice to have a warning about forgotten enum cases, but if the value was passed via a variable/parameter isn't it still possible that undefined values occur during run time? What about wrapping the default case between #ifndef ENUM_COVERAGE_TEST ... #endif – grenix May 18 '21 at 18:33
9

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

drdwilcox
  • 3,833
  • 17
  • 19
4

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.

Igor
  • 26,650
  • 27
  • 89
  • 114
  • 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 Nov 05 '11 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 Nov 05 '11 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 Nov 05 '11 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 Nov 20 '12 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 Jul 18 '16 at 12:36
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
riveridea
  • 53
  • 5
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aleroot
  • 71,077
  • 30
  • 176
  • 213
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.

RG1
  • 105
  • 1
  • 1
  • 11
0

The syntax for a switch statement in C programming language is as follows:

switch(expression) {

   case constant-expression  :
      statement(s);
      break; /* optional */

   case constant-expression  :
      statement(s);
      break; /* optional */

   /* you can have any number of case statements */
   default : /* Optional */
   statement(s);
}
Arif
  • 6,094
  • 4
  • 49
  • 81