int a = 10;
switch(a){
case 0:
printf(\"case 0\");
break;
case 1:
printf(\"case 1\");
break;
}
Is the above code valid?
If
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.
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);
}