c - 如何在 gcc 中正确使用 __attribute__((fallthrough))

标签 c gcc switch-statement fall-through

代码示例:

int main(int argc, char **argv)
{
    switch(argc)
    {
    case 0:
        argc = 5;
        __attribute__((fallthrough));

    case 1:
        break;
    }
}

Using gcc 6.3.0 ,只有-std=c11,这段代码给出警告:

<source>: In function 'main':
7 : <source>:7:3: warning: empty declaration
   __attribute__((fallthrough));
   ^~~~~~~~~~~~~

在不引发警告的情况下使用它的正确方法是什么?

最佳答案

如前所述,__attribute__ ((fallthrough)) 是在 GCC 7 中引入的。 要保持向后兼容性并清除 Clang 和 GCC 的失败警告,您可以使用/* fall through */ marker comment .

应用于您的代码示例:

int main(int argc, char **argv)
{
    switch(argc)
    {
    case 0:
        argc = 5;
        /* fall through */

    case 1:
        break;
    }

    return 0;
}

关于c - 如何在 gcc 中正确使用 __attribute__((fallthrough)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45349079/

相关文章:

c - 未知的 C 字符串截断/覆盖

gcc - x86_64 "gcc -S"-> as -> ld -> 执行失败

switch-statement - 错误 : value of type 'String' has no member 'hasSuffix' in Swift switch

c - 没有默认大小写的 Objective-C 枚举 "exhaustive"开关的行为

Java 验证循环未纠正最后一个错误

c - 这段带有指针函数的代码是如何工作的?

c - 错误 : use of undeclared identifier 'errno_t'

c - 根据 CPU 特性在 gcc 中选择合适的 ARM -mfpu 选项

c - 如何刷新用 stdio 函数编写的输出?

c - 声明结构以避免混合声明和代码