51

Here is a piece of code in /usr/src/linux-3.10.10-1-ARCH/include/linux/printk.h:

static inline int printk_get_level(const char *buffer)
{
  if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
    switch (buffer[1]) {
    case '0' ... '7':
    case 'd':  /* KERN_DEFAULT */
      return buffer[1];
    }
  }
}

Is it a kind of operator? Why does "The C Programming Language" not mention it?

syb0rg
  • 8,057
  • 9
  • 41
  • 81
jasonz
  • 1,315
  • 1
  • 17
  • 31
  • 3
    http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Case-Ranges.html#Case-Ranges and read also [Strange initializer expression?](http://stackoverflow.com/questions/18329206/strange-initializer-expression) – Grijesh Chauhan Sep 17 '13 at 15:14
  • @GrijeshChauhan The questions and the answers to the dup listed do not even remotely match this question. This questions asks what is this feature the dup asks how to convert from this feature. It is silly to list it as a dup. – Shafik Yaghmour Sep 18 '13 at 09:25
  • @ShafikYaghmour Is it? Indeed the answers posted here are different. Through question is answer nicely (and accepted) and I don't see any scope to add new one..I voting for reopen. Thanks! – Grijesh Chauhan Sep 18 '13 at 09:34
  • @ShafikYaghmour Ok I have voted to reopen we have to wait till more users vote it for reopen either OP or you or me have to rise a flag to reopen... no other choice. – Grijesh Chauhan Sep 18 '13 at 09:45

4 Answers4

66

This is a gcc extension called case ranges, this is how it is explained in the document:

You can specify a range of consecutive values in a single case label, like this:

case low ... high:

You can find a complete list of gcc extensions here. It seems like clang also supports this to try and stay compatible with gcc. Using the -pedantic flag in either gcc or clang will warn you that this is non-standard, for example:

warning: range expressions in switch statements are non-standard [-Wpedantic]

It is interesting to note that Linux kernel uses a lot of gcc extensions one of the extensions not covered in the article is statement expressions.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
12

It is gcc compiler extension allowing to combine several case statement in one line.

Vladimir
  • 170,431
  • 36
  • 387
  • 313
11

Beware, it is not standard C and therefore not portable. It is a shorthand devised for case statements. It's well-defined since in C you can only switch on integral types.

In standard C, ... is only used in variable length argument lists.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
6

case '0'...'7': is case ranges Speciacation in gcc.

Range specification for case statement.

Write spaces around the ..., for otherwise it may be parsed wrong when you use it with integer values

case '0' or case '1' or case '3' and so on case '7':
or case 'b' :
just return buffer[1]; 
Gangadhar
  • 10,248
  • 3
  • 31
  • 50