Rules of Using Switch Case in C Programming

February 13, 2025 No Comments » Hits : 75






Rules of Using Switch Case in C Programming

  1. Case Label must be unique
  2. Case Labels must ends with Colon
  3. Case labels must have constants / constant expression
  4. Case label must be of integral Type ( Integer,Character)
  5. Case label should not be ‘floating point number ‘
  6. Switch case should have at most one default label
  7. Default label is Optional
  8. Default can be placed anywhere in the switch
  9. Break Statement takes control out of the switch
  10. Two or more cases may share one break statement
  11. Nesting ( switch within switch ) is allowed.
  12. Relational Operators are not allowed in Switch Statement.
  13. Macro Identifier are allowed as Switch Case Label.
  14. Const Variable is allowed in switch Case Statement.
  15. Empty Switch case is allowed.

Syntax of Switch Case :

switch ( expression )
    {
      case label1 :
            body1
            break;
      case label2 :
            body2
            break;
      case label3 :
            body3
            break;
      default :
           default-body
           break;
 }
 next-statement;

Rule 1 : Case Label must be unique

int id = 3 ;
switch(id)
       {
       case 1:
               printf("C Programming Language");
               break;
       case 2:
               printf("C++ Programming Language");
               break;
       case 2:
               printf("Web Technology");
               break;
       default :
               printf("No student found");
               break;
        }

Rule 2 : Case Labels must ends with Colon

case 1 :
       printf("C Programming Language");
       break;

Rule 3 : Case labels must have constants / constant expression

case 1+1:
case 'A':
case 67:

these are allowed examples of switch case labels , however variables are not allowed in switch case labels.

case var :
case num1 :
case n1+n2 :

Rule 4 : Case label must be of integral Type ( Integer,Character) whereas Case label should not be ‘floating point number ‘

case 10:
case 20+20:
case 'A':
case 'a':

these are allowed examples and following are illegal examples -

case 10.12:
case 7.5:

Rule 5 : Switch case should have at most one default label

switch(roll)
       {
       case 1:
               printf("C Programming Language");
               break;
       case 2:
               printf("C++ Programming Language");
               break;
       case 2:
               printf("Web Technology");
               break;
       default :
               printf("Default Version 1");
               break;
       default :
               printf("Default Version 2");
               break;
        }

It violets first rule.

Rule 6 : Default label is Optional

switch(roll)
       {
       case 1 :
               printf("C Programming Language");
               break;
       case 2 :
               printf("C++ Programming Language");
               break;
       case 2 :
               printf("Web Technology");
               break;
        }

default statement is optional. It can be neglected.

Rule 7 : Default can be placed anywhere in the switch

switch(roll)
       {
       case 1 :
               printf("C Programming Language");
               break;
       default:
               printf("No Student Found");
               break;
       case 2 :
               printf("C++ Programming Language");
               break;
       case 2 :
               printf("Web Technology");
               break;
        }

Rule 8 : Break Statement takes control out of the switch

Rule 9 : Two or more cases may share one break statement

switch(alpha)
       {
       case 'a':
       case 'A':
               printf("Alphabet A");
               break;
       case 'b':
       case 'B':
               printf("Alphabet B");
               break;
        }

Rule 10 : Nesting ( switch within switch ) is allowed

switch(alpha)
       {
       case 'a':
       case 'A':
               printf("Alphabet A");
               break;
       case 'b':
       case 'B':
               switch(alpha)
               {
               }
               break;
        }

nesting of switch case is allowed in C.

Rule 11 : Relational Operators are not allowed in Switch Statement.

switch(num)
       {
       case >15:
               printf("Number > 15");
               break;
       case =15:
               printf("Number = 15");
               break;
       case <15:
               printf("Number < 15");
               break;
        }

relational operators are not allowed as switch label.

Rule 12 : Macro Identifier are allowed as Switch Case Label.

#define MAX 2
switch(num)
       {
       case MAX:
               printf("Number = 2");
               break;
        }

as preprocessor will replace occurrence of MAX by constant value i.e 2 therefor it is allowed.

Rule 13 : Const Variable is allowed in switch Case Statement.

int const var = 2;
switch(num)
       {
       case var:
               printf("Number = 2");
               break;
        }

Related Articles:

Leave A Response