How to do an explicit fall-through in C

一曲冷凌霜 提交于 2019-12-05 00:00:54

Use __attribute__ ((fallthrough))

switch (condition) {
    case 1: __attribute__ ((fallthrough));
    case 2: __attribute__ ((fallthrough));
    case 3:
        printf("1..3\n");
        break;
}

You should be able to use GCC diagnostic pragmas to disable that particular warning for your source file or some portion of a source file. Try putting this at the top of your file:

#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"

GCC fallghrough magic comments

You should not use this, it is insane, but good to know about:

int main(int argc, char **argv) {
    (void)argv;
    switch (argc) {
        case 0:
            argc = 1;
            // fall through
        case 1:
            argc = 2;
    };
}

prevents the warning on GCC 7.4.0 with:

gcc -Wall -Wextra main.c

man gcc describes how different comments may or not be recognized depending on the value of:

-Wimplicit-fallthrough=n

C++17 [[fallthrough]] attribute

C++17 got a standardized syntax for this: GCC 7, -Wimplicit-fallthrough warnings, and portable way to clear them?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!