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,517
  • 1
  • 41
  • 46
assem
  • 3,023
  • 4
  • 20
  • 20

3 Answers3

16

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
  • 486,780
  • 108
  • 951
  • 1,012
11

This is not standard C, see 6.8.4.2:

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

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
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
  • 510,854
  • 105
  • 1,084
  • 1,005