如何在c的case语句中使用省略号?
CASE expr_no_commas ELLIPSIS expr_no_commas ':'
我在c的语法规则中看到了这样的规则,但是当我尝试重现它时:
int test(float i) { switch(i) { case 1.3: printf("hi"); } }
它失败…
好吧,这涉及我的一些猜测,但看起来你正在谈论C的gcc
扩展,允许在switch
情况下指定范围。
以下编译对我来说:
int test(int i) { switch(i) { case 1 ... 3: printf("hi"); } }
注意...
还要注意你不能打开float
。
这不是标准C,见6.8.4.2:
每个case标签的表达式应为整数常量表达式
ELLIPSIS意味着...
,而不是.
。 声明应该是:
#include int main() { int x; scanf("%d", &x); switch (x) { case 1 ... 100: printf("1 <= %d <= 100\n", x); break; case 101 ... 200: printf("101 <= %d <= 200\n", x); break; default: break; } return 0; }
顺便说一下,这是gcc
的非标准扩展 。 在标准C99中我找不到这种语法。