How to use __attribute__((fallthrough)) correctly in gcc

M.M picture M.M · Jul 27, 2017 · Viewed 13.1k times · Source

Code sample:

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

    case 1:
        break;
    }
}

Using gcc 6.3.0, with -std=c11 only, this code gives a warning:

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

What is the correct way to use this without eliciting a warning?

Answer

jyvet picture jyvet · Feb 9, 2018

As previously answered, __attribute__ ((fallthrough)) was introduced in GCC 7. To maintain backward compatibility and clear the fall through warning for both Clang and GCC, you can use the /* fall through */ marker comment.

Applied to your code sample:

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

    case 1:
        break;
    }

    return 0;
}