What if I don't write default in switch case?

CSwitch Statement

C Problem Overview


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.

C Solutions


Solution 1 - C

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.

Solution 2 - C

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.

Solution 3 - C

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

Solution 4 - C

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.

Solution 5 - C

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.

Solution 6 - C

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.

Solution 7 - C

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.

Solution 8 - C

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeegar PatelView Question on Stackoverflow
Solution 1 - CCB BaileyView Answer on Stackoverflow
Solution 2 - CPraetorianView Answer on Stackoverflow
Solution 3 - CdrdwilcoxView Answer on Stackoverflow
Solution 4 - CIgorView Answer on Stackoverflow
Solution 5 - CriverideaView Answer on Stackoverflow
Solution 6 - CalerootView Answer on Stackoverflow
Solution 7 - CRG1View Answer on Stackoverflow
Solution 8 - CArifView Answer on Stackoverflow