Search code examples
csuppress-warnings

Best way to suppress C warning "Unused variable x"?


What is the best/neatest way to suppress a C compiler (for example GCC) like "Unused variable x" warning?

GCC's doumentation explains

-Wunused-variable
Warn whenever a local variable or non-constant static variable is unused aside from its declaration

I don't want to give any certain flags to my compiler to remove all these warnings, just for special cases.


Solution

  • I found an article, http://sourcefrog.net/weblog/software/languages/C/unused.html, that explains UNUSED. It is interesting that the author also mangles the unused variable name, so you can't inadvertently use it in the future.

    Excerpt:

    #ifdef UNUSED
    #elif defined(__GNUC__)
    # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
    #elif defined(__LCLINT__)
    # define UNUSED(x) /*@unused@*/ x
    #else
    # define UNUSED(x) x
    #endif
    
    void dcc_mon_siginfo_handler(int UNUSED(whatsig))