5
CASE expr_no_commas ELLIPSIS expr_no_commas ':'

I saw such a rule in c's syntax rule,but when I try to reproduce it:

int test(float i)
{
switch(i)
{
  case 1.3:
    printf("hi");
}
}

It fails...

a3f
  • 8,227
  • 1
  • 36
  • 42
assem
  • 2,863
  • 4
  • 19
  • 19

3 Answers3

15

OK, this involves a bit of guesswork on my part, but it would appear that you're talking about a gcc extension to C that allows one to specify ranges in switch cases.

The following compiles for me:

int test(int i)
{
  switch(i)
  {
  case 1 ... 3:
    printf("hi");
  }
}

Note the ... and also note that you can't switch on a float.

NPE
  • 464,258
  • 100
  • 912
  • 987
11

This is not standard C, see 6.8.4.2:

The expression of each case label shall be an integer constant expression

Lindydancer
  • 24,366
  • 4
  • 47
  • 67
10

ELLIPSIS means ..., not .. The statement should be like:

#include <stdio.h>

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;    
}

BTW, this is a non-standard extension of gcc. In standard C99 I cannot find this syntax.

kennytm
  • 491,404
  • 99
  • 1,053
  • 989