Basic and Advance C Question:
Download Questions PDF

Is there a way to have non-constant case labels (i.e. ranges or arbitrary expressions)?

Answer:

No. The switch statement was originally designed to be quite simple for the compiler to translate, therefore case labels are limited to single, constant, integral expressions. You can attach several case labels to the same statement, which will let you cover a small range if you don't mind listing all cases explicitly.
If you want to select on arbitrary ranges or non-constant expressions, you'll have to use an if/else chain.

Download C Programming Interview Questions And Answers PDF

Previous QuestionNext Question
Are the outer parentheses in return statements really optional?Is there a way to switch on strings?