cgccc99gcc-warning

How to do an explicit fall-through in C


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 to allow fall throughs from all case-statements.

Is there a way to do an explicit fall through? I'd prefer to avoid having to compile with Wno-implicit-fallthrough for this file.

EDIT: I'm looking for a way to make the fall through explicit (if it's possible), not to turn off the warning via a compiler switch or pragma.


Solution

  • Use __attribute__ ((fallthrough))

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