Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
639 views
in Technique[技术] by (71.8m points)

gcc - What is "..." in switch-case in C code

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...