C Programming

Data type of case labels of switch statement

In C switch statement, the expression of each case label must be an integer constant expression.

For example, the following program fails in compilation.

/* Using non-const in case label */
#include<stdio.h>
int main()
{
  int i = 10;
  int c = 10;
  switch(c) 
  {
    case i: // not a "const int" expression
         printf("Value of c = %d", c);
         break;
    /*Some more cases */

  }
  getchar();
  return 0;
}

Putting const before i makes the above program work.

#include<stdio.h>
int main()
{
  const int i = 10;
  int c = 10;
  switch(c) 
  {
    case i:  // Works fine
         printf("Value of c = %d", c);
         break;
    /*Some more cases */

  }
  getchar();
  return 0;
}

Recent Comments

Feedback

For any feedback,email us at feedback@geeksforgeeks.org

Report a Bug

For any report or bug,email us at support@geeksforgeeks.org