RSS
Follow Us
Contribute
 
 
 
 

Error C2051: case expression not constant

 
We need YOUR SKILLS in this collaborative work, to build the largest solution center for error messages.

From $1

Table of contents
  1. 1. Cause
  2. 2. Solution

Cause

This error message appears when a constant is not used in "case" expression. The common mistake is using variables. Ex:

int main()
{
	char sw = 'b';
      char ch = 'a';
	int c;
	switch (sw){               
		case ch:          //This line generates C2051  
			c = 0;
	                break;
              default:
			c = 1;
	                break;
      }
}

Solution

Use a constant for "case" expression.

int main()
{
	char sw = 'b';
	int c;
	switch (sw){               
		case 'a':
			c = 0;
	                break;
              default:
			c = 1;
	                break;
      }
}

 
 
 
 
Comments