Sunday, January 22, 2012

C Switch Case Questions

1.int main()
{
int i=3;
switch(i)
 {
    default:printf("zero")
    case 1: printf("one");
   break;
   case 2:printf("two");
  break;
  case 3: printf("three");
  break;
  } 
}
Output:three
Explanation:The default case can be placed anywhere inside the loop. It is executed
only when all other cases doesn't match.

2.#include<stdio.h>
main()
{
int i=1,j=2;
switch(i)
 {
 case 1:  printf("GOOD");
    break;
 case j:  printf("BAD");
      break;
 }
}
Output:Compiler Error: Constant expression required in function main.
Explanation:The case statement  can have only constant  expressions  (this  implies
that we cannot use variable names directly so an error).
Note:Enumerated types can be used in case statements.

No comments:

Post a Comment