We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi. I used the following code, and appeared the error "error C110: non-constant case/dim expresion" at the "case" line, what does it mean? ...
void main(void){ char command[]={1,2,3,4,5,6,7,8}; char word; //........ while(1){ /* ......... some lines of code */ switch(word){ case command[0]: // do somthing break; //................... case command[7]: // do somthing break; } } }
Thank you.
It means that your code is not valid C.
In a switch statement, every case must be constant value - not a variable or a function call. All cases must also be unique.
A constant value may be a numeric value (15), a character constant ('A'), a hexadecimal constant (0x10) etc.
But I also tryed this, and the same error:
const char[]={1,2,3,4,5};
As PerSo Westermark said, "every case must be constant value - not a variable..."
It has nothing to do with the const keyword. It has to do with not being a variable or function.
enum { CMD1 = 1, CMD2, CMD3, ... }; switch (command) { case CMD1: // handle command 1. break; case CMD2: // handle command 2. break; ... default: // Unknown command. ; }
"6.8.4.2 The switch statement ... 3 The expression of each case label shall be an integer constant expression and no two of the case constant expressions in the same switch statement shall have the same value after conversion. ..."
"6.6 Constant expressions ... 6 An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof operator. ..."
Bold emphasis is mine.
Thank you for details and helping me... Have a nice day or good night - where you are...
Damian.