65

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.

4
  • 12
    There are legitimate reasons to do this, e.g. take plugin development for a system, that expects your function with a specific signature like void function_name(int par1, char *par2); and you only need to work on par2. Commented Feb 7, 2012 at 17:04
  • 6
    In that case, write the signature as void function_name(int par1, char*); which is perfectly valid and won't generate warnings. Commented Feb 18, 2013 at 13:52
  • 6
    @spaceknarf If I do that with gcc 4.7.2 I get error: parameter name omitted. Commented Feb 23, 2013 at 1:18
  • 1
    I don't think it is valid to leave a parameter unnamed in a C function signature when it's part of the definition. It's acceptable in C++ though. Commented Sep 28, 2016 at 20:11

11 Answers 11

54

(void) variable might work for some compilers.

For C++ code, also see Mailbag: Shutting up compiler warnings where Herb Sutter recommends using:

template<class T> void ignore( const T& ) { } ... ignore(variable); 
Sign up to request clarification or add additional context in comments.

9 Comments

This works on gcc 4.7.1 with -std=c99
And it works on VS as well.
(void) works not with nvcc 7.5 with gcc 4.9.2 and C++11 enabled. The herbsutter link with template<class T> void ignore( const T& ) { } works great!
The original question is tagged as C, not C++. As far as I know the (void)var works in all major C compilers.
(void) variable is cool because it works for any unused variables, not just function parameters like with the accepted answer.
|
51

Tell your compiler using a compiler specific nonstandard mechanism

See individual answers for __attribute__((unused)), various #pragmas and so on. Optionally, wrap a preprocesor macro around it for portability.

Switch the warning off

IDEs can signal unused variables visually (different color, or underline). Having that, compiler warning may be rather useless.

In GCC and Clang, add -Wno-unused-parameter option at the end of the command line (after all options that switch unused parameter warning on, like -Wall, -Wextra).

Add a cast to void

void foo(int bar) { (void)bar; } 

As per jamesdlin's answer and Mailbag: Shutting up compiler warnings.

Do not give the variable a name (C23 and C++ only)

Not allowed in C before the C23 standard, but with a latest compiler (in 2023) and in in C++ (since like forever) one can do

void foo(int /*bar*/) { ... } 

See the N2480 Allowing unnamed parameters in a function definition (pdf) proposal, and check the implementation status at https://en.cppreference.com/w/c/compiler_support

GCC 11, Clang 11, and ICX 2022.2 (oneAPI 2022.3) support this.

1 Comment

I can't believe commenting it out works. That's the way to go if you're using C++ and it is compliant.
25

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)) 

5 Comments

... another write-only header macro is just what we need to get a rather benign warning out of the way.
I'm with you. My code contains static const char cvsid[] = "$Id$";. That's short and sweet and readable, but gcc -Wall throws a wobbly on it. I don't want to have to write all this impenetrable code just to suppress that error.
The link is broken: "Error: Not Found. The requested URL /weblog/software/languages/C/unused.html was not found on this server."
Re "UNUSED": Don't you mean unused (the GCC thingy)?
In any case, can you link to documentation? (But without "Edit:", "Update:", or similar - the answer should appear as if it was written today.)
20

If this is really what you want, you could use the unused attribute (GCC only), something like:

void foo(int __attribute__((__unused__)) bar) { ... } 

Not just for function parameters, of course, but that's the most common use case, since it might be a callback function for an API where you don't actually need all the input.

Additionally, GLib has a G_GNUC_UNUSED macro which I believe expands to that attribute.

Comments

14

You can silence the warning using #pragma

#pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused" int unususedVariable = 1; #pragma clang diagnostic pop 

If you are using GCC, use #pragma gcc ...

2 Comments

Maybe the last line should be #pragma clang diagnostic pop
#pragma GCC ... (it wants upper case GCC)
4

#pragma unused <variable>

3 Comments

Not portable - generates "unknown pragma" warnings on many compilers
Paul: That is true and valid, but I'm assuming it doesn't need to be portable since the only reason I can think of to have unused variables is because you have some code commented out for debugging and you still want your (temporary) debugging build to only show warnings you weren't expecting.
Using this in Xcode4.0 with LLVM 3.0/GCC 4.2 will require you to use parens. #pragma unused (variable)
4

The cast to a void is the best approach because it shows that you didn't "accidentally" keep the variable in your code - ie: this function might be an instance where you have a table of function pointers that need the same parameter types and return types, but in this particular table entry you are not using the parameter.

That said, if you don't need it, get rid of it. ;)

1 Comment

Can you add an example (that actually compiles (with some specific version of one or more specific compilers))? (But without "Edit:", "Update:", or similar - the answer should appear as if it was written today.)
4

It's a very hackish solution, but try simply assigning the variable to itself.

I think that should fool most compilers into thinking that the variable is used. It should be quite portable too.

3 Comments

It does fool the compiler into thinking it's used, but you may then get a "code has no effect" warning, depending on the compiler and options.
the = operator might be overloaded with side-effects. you never know. cast to void seems better. eg (void)my_unused_thing;
Clang (and possibly GCC) now has a warning to catch exactly this by now, btw.
0

Assign it to itself:

void f(int unused) { unused = unused; } 

It works in GCC, but Clang needs -Wno-self-assign.


I think casting to void is the most portable solution: Both GCC and Clang understand this, even with full warnings -W{all,extra,pedantic}:

(void)unused; 

Comments

-9

Delete the unused variable declaration from the code (pun intended).

(What??? It's what I do: point that the obvious is most likely the best solution.)

Now, from comments on other answers, apparently it's garbage generated from macros. Well, that's a pleonasm.

Solutions:

  • refactor that macro to #if declare the variable only if it's really used;
  • create another version of the macro that skips the unused variable generation.
  • Better still, avoid using macros that bring issues to the code.

6 Comments

Not possible in this case. It is a macro that "spits out" the variable and I don't want to use it in this case.
There's other cases as well - e.g if you need to provide a callback function to an external API, but you don't really use/care about some of the parameters you'll get warnings
nos: Thank you for not assume I'm totally stupid like many of the comments above.
write more precise questions and you'll get more precise answers
-1, no substance in the answer. Not even trying to understand the real problem. nos, good comment, +1
|
-10

If it’s used and you are shipping the project, delete it. Worst, comment it.

1 Comment

I have these warnings, because the variables are assigned via ASM, so they are not unused but my compiler does not know ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.