Search code examples
cswitch-statement

How to use ellipsis in c's case statement?


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...


Solution

  • 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.