How to do an explicit fall-through in C

dlasalle picture dlasalle · Jun 13, 2017 · Viewed 8.8k times · Source

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.

Answer

Sergey Kalinichenko picture Sergey Kalinichenko · Jun 13, 2017

Use __attribute__ ((fallthrough))

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