How to do an explicit fall-through in C

后端 未结 3 1661
遇见更好的自我
遇见更好的自我 2021-01-03 22:45

The newer versions of gcc offer the Wimplicit-fallthrough, which is great to have for most switch statements. However, I have one switch statement where I want

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-03 22:52

    GCC fallghrough magic comments

    You should not use this if you can help it, 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?

提交回复
热议问题