62

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.

usr1234567
  • 21,601
  • 16
  • 108
  • 128
gustaf
  • 776
  • 1
  • 6
  • 6
  • 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. – Residuum Feb 07 '12 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. – Frank Kusters Feb 18 '13 at 13:52
  • 6
    @spaceknarf If I do that with gcc 4.7.2 I get `error: parameter name omitted`. – craig65535 Feb 23 '13 at 01: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. – davidA Sep 28 '16 at 20:11

11 Answers11

52

(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);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • This works on gcc 4.7.1 with `-std=c99` – Wayne Conrad May 02 '13 at 16:59
  • And it works on VS as well. – AndersK Dec 17 '13 at 20:54
  • (void) works not with nvcc 7.5 with gcc 4.9.2 and C++11 enabled. The herbsutter link with `template void ignore( const T& ) { }` works great! – Ax3l Jan 21 '16 at 16:21
  • 1
    The original question is tagged as C, not C++. As far as I know the (void)var works in all major C compilers. – Calmarius Mar 18 '16 at 13:46
  • might work in SOME compilers? know a single compiler where it doesn't work? – hanshenrik Nov 09 '16 at 13:15
  • @hanshenrik: Yes, see the comments in the linked article: reportedly EDG-based compilers. – jamesdlin Nov 09 '16 at 21:25
  • 3
    `(void) variable` is cool because it works for any unused variables, not just function parameters like with the accepted answer. – vgru Jul 11 '17 at 10:37
  • The question is about C and not C++. – usr1234567 Apr 08 '23 at 07:16
  • @usr1234567 I gave an answer for C. (IIRC, at the time this was the *only* answer that suggested the C idiom of `(void) variable`.) Even though it wasn't what the OP asked for, I *additionally* gave an answer for C++ because other readers tend to have a bad practice of using C idioms for C++ even when there are better C++ ones. – jamesdlin Apr 08 '23 at 14:42
47

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.

user7610
  • 25,267
  • 15
  • 124
  • 150
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))
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Edward Leno
  • 6,257
  • 3
  • 33
  • 49
  • 8
    ... another write-only header macro is just what we need to get a rather benign warning out of the way. – jpinto3912 Aug 05 '10 at 21:24
  • 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. – Edward Falk May 26 '16 at 19:27
  • The link is broken: *"Error: Not Found. The requested URL /weblog/software/languages/C/unused.html was not found on this server."* – Peter Mortensen Nov 14 '21 at 23:04
  • Re *"UNUSED"*: Don't you mean *unused* (the GCC thingy)? – Peter Mortensen Nov 14 '21 at 23:06
  • In any case, can you link to documentation? (But ***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today.) – Peter Mortensen Nov 14 '21 at 23:26
18

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cascabel
  • 479,068
  • 72
  • 370
  • 318
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 ...

James Webster
  • 31,873
  • 11
  • 70
  • 114
4

#pragma unused <variable>

nmichaels
  • 49,466
  • 12
  • 107
  • 135
  • 8
    Not portable - generates "unknown pragma" warnings on many compilers – Paul R Aug 05 '10 at 18:35
  • 1
    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. – nmichaels Aug 05 '10 at 19:38
  • 4
    Using this in Xcode4.0 with LLVM 3.0/GCC 4.2 will require you to use parens. `#pragma unused (variable)` – tgunr Jan 27 '12 at 20:08
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mhmmd
  • 1,483
  • 9
  • 15
  • 5
    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. – Wayne Conrad May 02 '13 at 17:03
  • the `=` operator might be overloaded with side-effects. you never know. cast to void seems better. eg `(void)my_unused_thing;` – orion elenzil Aug 26 '20 at 18:16
  • Clang (and possibly GCC) now has a warning to catch exactly this by now, btw. – c-x-berger May 27 '21 at 19:29
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. ;)

  • 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.) – Peter Mortensen Nov 14 '21 at 23:15
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;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2394284
  • 5,520
  • 4
  • 32
  • 38
-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.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jpinto3912
  • 1,457
  • 2
  • 12
  • 19
  • 1
    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. – gustaf Aug 05 '10 at 18:25
  • 14
    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 Aug 05 '10 at 18:26
  • 1
    nos: Thank you for not assume I'm totally stupid like many of the comments above. – gustaf Aug 05 '10 at 18:30
  • 2
    write more precise questions and you'll get more precise answers – KeatsPeeks Aug 05 '10 at 18:38
  • 2
    -1, no substance in the answer. Not even trying to understand the real problem. nos, good comment, +1 – Daniel Wedlund Aug 06 '10 at 07:53
  • @Daniel, thanks, because the comments got me false credits, and I was just intending a pun... also, of course I can't understand the real problem from that 2-phrase question. But I'll edit to provide meaningful solution. – jpinto3912 Aug 06 '10 at 08:37
-10

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Praveen S
  • 10,355
  • 2
  • 43
  • 69
  • 1
    I have these warnings, because the variables are assigned via ASM, so they are not unused but my compiler does not know ... – Tarion Apr 10 '17 at 12:50