问题描述
在 c(99?) 或目标 C switch 语句中是否有支持范围大小写的技巧?我知道不支持这样写:
Is there a hack to support range case in a c(99?) or objective C switch statement ? I know this is not supported to write something like this:
switch(x)
case 1:
case 2..10:
case 11:
但我在想应该有一种方法可以使用#define 宏生成代码.当然我可以用案例列表定义一个宏,但我希望有一种更优雅的方式,比如CASERANGE(x,x+10) 会生成:
But I was thinking there should be a way to generate code with a #define macro. Of course I can define a macro with the list of cases but I was hoping for a more elegant way like CASERANGE(x,x+10) which would generate:
case x
case x+1
case x+2
有可能吗?
推荐答案
GCC 有一个 对 C 语言的扩展 允许与您的第一个示例类似的内容,但除此之外,如果有可移植/ANSI 的方式来实现它,那么现在就已经完成了.我不相信有一个.
GCC has an extension to the C language that allows something similar to your first example, but other than that, if there was a portable/ANSI way of doing it, it would have been done by now. I don't believe there is one.
这篇关于c中的模拟器/生成的switch语句范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!