什么是" ..."在C code开关情况 [英] What is "..." in switch-case in C code

查看:238
本文介绍了什么是" ..."在C code开关情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是 /usr/src/linux-3.10.10-1-ARCH/include/linux/printk.h 一块code的:

 静态内联INT printk_get_level(为const char *缓冲区)
{
  如果(缓冲[0] == KERN_SOH_ASCII&放大器;&放大器;缓冲液[1]){
    开关(缓冲液[1]){
    为'0'...'7':
    案D:/ * * KERN_DEFAULT /
      返回缓冲液[1];
    }
  }
}

它是一种运营商?为什么C程序设计语言不提了?


解决方案

这是一个 GCC extension称为case范围,这是它是如何在文件中解释说:


  

您可以在一个单一的情况下,标签指定一个连续值的范围,如:

 情况下低...高:


您可以在这里找到 GCC扩展的完整列表。这似乎是铛也支持这种,试图保持兼容 GCC 。在使用 -pedantic 标记或者 GCC 将提醒你,这是不规范的,例如:

 警告:在switch语句范围前pressions非标准[-Wpedantic]

这是值得注意href=\"http://www.ibm.com/developerworks/library/l-gcc-hacks/\"> Linux内核使用了大量的GCC扩展中的一个

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?

解决方案

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.

这篇关于什么是" ..."在C code开关情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆