What is the "..." in the switch in C code

Here is the code snippet 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 this a kind of operator? Why doesn't C programming language mention this?

+49
c gcc gcc-extensions
Sep 17 '13 at 15:10
source share
4 answers

This is the gcc gcc extensions extension . Looks like clang also supports this in order to try and stay compatible with gcc . Using the -pedantic flag in gcc or clang warns you that this is a non-standard value, for example:

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

It is interesting to note that the Linux kernel uses many gcc extensions , one of the extensions not described in the article is an operator expression.

+62
Sep 17 '13 at 15:12
source share

This is a gcc extension compiler that allows you to combine several case statements on the same line.

+12
Sep 17 '13 at 15:11
source share

Beware, it is not standard C and therefore is not portable. This is a summary of the case statements. It is well defined, since in C you can only include integral types.

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

+10
Sep 17 '13 at 15:12
source share

case '0' ... '7': Speciacation range range in gcc.

Range specification for the case statement.

Record spaces around ... otherwise it may not be parsed correctly 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]; 
+6
Sep 17 '13 at 15:13
source share



All Articles