Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

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

0
Posted

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

0

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. See also questions 20.16 and 20.17. References: K&R1 Sec. 3.4 p. 55 K&R2 Sec. 3.4 p. 58 ISO Sec. 6.6.4.2 Rationale Sec. 3.6.4.2 H&S Sec. 8.7 p. 248 comp.lang.c FAQ list ยท Question 20.19 Q: Are the outer parentheses in return statements really optional? A: Yes. Long ago, in the early days of C, they were required, and just enough people learned C then, and wrote code which is still in circulation, that the notion that they might still be required is widespread. (As it happens, parentheses are optional with the sizeof operator, too, under certain circumstances.) Refe

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123