Switch case MCQ 18: Both Int and Char in same switch is allowed



Switch case MCQ 18: Both Int and Char in same switch is allowed


Problem Statement 18 : Both Int and Char in same switch is allowed

void main()
{
 int choice = -2 ;
  switch(choice)
  {
  case 1:
          printf("\nCase -1");
          break;
  case '1':
          printf("\nCase -2");
          break;
  }
}

Options : Is it working properly ?

  1. Yes
  2. No

[toggle title=”Output”]Yes[/toggle]


How ? Why ?

  1. Generally Case Label Should be Integer / Constant
  2. Characters are Converted into Integer internally using  ASCII
  3. Here there will be two case in above Program i.e Case 1 & case 49
  4. 49 is the ASCII value of ‘1’