267

For instance:

Bool NullFunc(const struct timespec *when, const char *who)
{
   return TRUE;
}

In C++ I was able to put a /*...*/ comment around the parameters. But not in C of course, where it gives me the error:

error: parameter name omitted

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nixgadget
  • 6,983
  • 16
  • 70
  • 103

13 Answers13

385

I usually write a macro like this:

#define UNUSED(x) (void)(x)

You can use this macro for all your unused parameters. (Note that this works on any compiler.)

For example:

void f(int x) {
    UNUSED(x);
    ...
}
SO Stinks
  • 3,258
  • 4
  • 32
  • 37
mtvec
  • 17,846
  • 5
  • 52
  • 83
  • 80
    I just use (void)x directly – Prof. Falken May 30 '12 at 11:27
  • 8
    while this is the only portable way AFAIK, the annoyance with this is it can be misleading if you use the variable later and forget ro remove the unused line. this is why GCC's __unused__ is nice. – ideasman42 Nov 07 '12 at 13:58
  • 1
    @Jog For example, *void f(int UNUSED(x))*, there are compile errors: "variable or field ‘foo’ declared void" and "expected primary-expression before ‘int’". – Yantao Xie Jan 17 '13 at 05:57
  • 6
    @CookSchelling: Ah but you shouldn't use it like that. Do something like this: `void f(int x) {UNUSED(x);}`. – mtvec Jan 17 '13 at 07:00
  • +1, nice. But I wonder if the parameters are not used at all, why not remove them from the function definition? – Alcott Feb 19 '14 at 05:25
  • 13
    @Alcott because (as in my case) the function might be one of many that have to have the same signature because they are referenced by a function pointer. – josch Sep 19 '14 at 05:50
  • @josch: Could you provide the exact command line you use and your version of GCC? I don't seem to be able to reproduce that warning. – mtvec Sep 19 '14 at 11:35
  • 1
    @Job turned out I was using (void*) instead of (void). The latter works just as you wrote. Deleting my comment. – josch Sep 19 '14 at 14:50
  • 29
    I'm using `#define UNUSED(...) (void)(__VA_ARGS__)` which allows me to apply this to multiple variables. – Matthew Mitchell Nov 13 '14 at 18:31
  • 6
    If using multiple variables, don't you run into *"warning: left-hand operand of comma expression has no effect"* ? – Liviu Chircu Oct 28 '15 at 11:52
  • @LiviuChircu clang 4.0 will give you `warning : expression result unused [-Wunused-value]` – diapir Dec 06 '16 at 11:22
  • 1
    KEIL 8051 keeps complaining unfortunately, so `#define UNUSED(x) ((x)=(x))` worked better for – Oleg Kokorin Dec 05 '17 at 13:41
  • I mean why would I go through the macro torture when I could just do `(void)argsv` Programming is to make life better not the other way. – pasignature Mar 02 '20 at 16:07
  • 7
    @pasignature: I'd say to make your code self-explanatory. `UNUSED(argsv)` says what it does, `(void)argsv` does not in my opinion. – mtvec Mar 03 '20 at 12:34
  • @OlegKokorin Thanks to your comment, I started [a library](https://github.com/mentalisttraceur/c-unused) to gather and automatically pick the right `UNUSED` implementation based on compiler. With Keil's compilers, we can use `#ifdef __KEIL__`. If you or anyone else knows of others which need special handling or benefits from a different solution, issues or PRs are welcome! – mtraceur May 15 '22 at 07:03
146

In GCC, you can label the parameter with the unused attribute.

This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC will not produce a warning for this variable.

In practice this is accomplished by putting __attribute__ ((unused)) just before the parameter. For example:

void foo(workerid_t workerId) { }

becomes

void foo(__attribute__((unused)) workerid_t workerId) { }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Philip Potter
  • 8,975
  • 2
  • 37
  • 47
  • 27
    For any newbies like me, this means putting `__attribute__ ((unused))` in front of the argument. – josch Sep 19 '14 at 05:53
  • 2
    @josch I think you are totally correct, but the documentation seems to imply that it should be put *after* the parameter. Both options are probably supported without problems. – Antonio Apr 24 '15 at 09:43
  • 2
    Also note that `__attribute__((unused))` is [a proprietary GCC extension](https://stackoverflow.com/a/47518775/6296561). It's supported by some other compilers, but I assume this won't work with MSVC. It's not directly a part of the compiler standard though, so this isn't as portable as some other options – Zoe Aug 23 '19 at 20:51
  • 9
    Calling an extension within GCC "proprietary" is, uh, well it's something. – c-x-berger May 27 '21 at 18:54
  • As a small generalization, `__attribute__ ((unused)) int myUnusedFunc()` gives no unused function warnings – Alex Li May 03 '22 at 22:16
73

You can use GCC or Clang's unused attribute. However, I use these macros in a header to avoid having GCC specific attributes all over the source, also having __attribute__ everywhere is a bit verbose/ugly.

#ifdef __GNUC__
#  define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
#else
#  define UNUSED(x) UNUSED_ ## x
#endif

#ifdef __GNUC__
#  define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_ ## x
#else
#  define UNUSED_FUNCTION(x) UNUSED_ ## x
#endif

Then you can do...

void foo(int UNUSED(bar)) { ... }

I prefer this because you get an error if you try use bar in the code anywhere, so you can't leave the attribute in by mistake.

And for functions...

static void UNUSED_FUNCTION(foo)(int bar) { ... }

Note 1):

As far as I know, MSVC doesn't have an equivalent to __attribute__((__unused__)).

Note 2):

The UNUSED macro won't work for arguments which contain parenthesis,
so if you have an argument like float (*coords)[3] you can't do,
float UNUSED((*coords)[3]) or float (*UNUSED(coords))[3]. This is the only downside to the UNUSED macro I found so far, and in these cases I fall back to (void)coords;.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ideasman42
  • 42,413
  • 44
  • 197
  • 320
  • Or maybe just `#define __attribute__(x)` for non-GCC environment (AFAIK none of the `__attribute__` are supported by MSVC)? – Franklin Yu Nov 10 '17 at 06:16
  • 1
    That can work, but dunder prefixed terms are reserved for the compiler so I'd rather avoid this. – ideasman42 Nov 10 '17 at 11:22
  • For my gcc at least putting the attribute specifier before the identifier seems to work right for funcs, vars, and parameter, so something like #define POSSIBLY_UNUSED(identifier) __attribute__((__unused__)) identifier can be used for all three – Britton Kerin Mar 02 '18 at 22:01
  • When putting it after I get `warning: unused parameter ‘foo’ [-Wunused-parameter]` (gcc 7.3.0) – ideasman42 Mar 03 '18 at 08:39
  • UNREFERENCED_PARAMETER(p) is defined in WinNT.h – david Jun 20 '21 at 22:27
32

Seeing that this is marked as gcc you can use the command line switch Wno-unused-parameter.

For example:

gcc -Wno-unused-parameter test.c

Of course this effects the whole file (and maybe project depending where you set the switch) but you don't have to change any code.

Paul Hutchinson
  • 1,578
  • 15
  • 21
24

With GCC with the unused attribute:

int foo (__attribute__((unused)) int bar) {
    return 0;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Teddy
  • 993
  • 10
  • 20
14

A gcc/g++ specific way to suppress the unused parameter warning for a block of source code is to enclose it with the following pragma statements:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
<code with unused parameters here>
#pragma GCC diagnostic pop
Calm
  • 341
  • 3
  • 4
  • 1
    Clang supports these diagnostic pragmas as well https://clang.llvm.org/docs/UsersManual.html#controlling-diagnostics-via-pragmas – eush77 Apr 28 '17 at 15:49
10

Since C++ 17, the [[maybe_unused]] attribute can be used to suppress warnings about unused parameters.

Based on the OP's example code:

Bool NullFunc([[maybe_unused]] const struct timespec *when, [[maybe_unused]] const char *who)
{
   return TRUE;
}
keith
  • 5,122
  • 3
  • 21
  • 50
  • 1
    Note the question specifies C and not C++. This answer will work fine in C++. For anyone tempted to try this with plain old C it will compile without warning (at least using GCC) so it 'works', but tools like clang-tidy will hate it. – chrBrd Dec 14 '21 at 03:25
7

I got the same problem. I used a third-part library. When I compile this library, the compiler (gcc/clang) will complain about unused variables.

Like this

test.cpp:29:11: warning: variable 'magic' set but not used [-Wunused-but-set-variable] short magic[] = {

test.cpp:84:17: warning: unused variable 'before_write' [-Wunused-variable] int64_t before_write = Thread::currentTimeMillis();

So the solution is pretty clear. Adding -Wno-unused as gcc/clang CFLAG will suppress all "unused" warnings, even thought you have -Wall set.

In this way, you DO NOT NEED to change any code.

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
landerlyoung
  • 1,693
  • 17
  • 14
  • 2
    This is fine if you actually want to ignore all unused warnings, but that's almost never the case. It's usually just specific instances you want to ignore. – Dan Bechard Nov 05 '16 at 18:07
  • Your problem is different, in any case. This question is about "unused parameter" warning while you got "unused variable" warning. – user7610 Apr 11 '23 at 08:19
5

Labelling the attribute is ideal way. MACRO leads to sometime confusion. and by using void(x),we are adding an overhead in processing.

If not using input argument, use

void foo(int __attribute__((unused))key)
{
}

If not using the variable defined inside the function

void foo(int key)
{
   int hash = 0;
   int bkt __attribute__((unused)) = 0;

   api_call(x, hash, bkt);
}

Now later using the hash variable for your logic but doesn’t need bkt. define bkt as unused, otherwise compiler says'bkt set bt not used".

NOTE: This is just to suppress the warning not for optimization.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
user2452561
  • 137
  • 2
  • 5
3

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.

Use a standard attribute (C23, C++17)

In C++17, there is the [[maybe_unused]] attribute which has become part of the standard. Before that, there was [[gnu::unused]]. See clang docs for additional overview. (The gnu:: attributes in general work in clang as well.)

As part of the C standardization effort bringing closer together C and C++ features, in C23 we get new attributes in the C language. One of them is [[maybe_unused]] which works the same as the C++ version. The [[gnu::unused]] compiler-specific attribute is not available in versions prior to C23, because earlier C language versions did not have these attributes at all.

user7610
  • 25,267
  • 15
  • 124
  • 150
1

In MSVC to suppress a particular warning it is enough to specify the it's number to compiler as /wd#. My CMakeLists.txt contains such the block:

If (MSVC)
    Set (CMAKE_EXE_LINKER_FLAGS "$ {CMAKE_EXE_LINKER_FLAGS} / NODEFAULTLIB: LIBCMT")
    Add_definitions (/W4 /wd4512 /wd4702 /wd4100 /wd4510 /wd4355 /wd4127)
    Add_definitions (/D_CRT_SECURE_NO_WARNINGS)
Elseif (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_GNUC)
    Add_definitions (-Wall -W -pedantic)
Else ()
    Message ("Unknown compiler")
Endif ()

Now I can not say what exactly /wd4512 /wd4702 /wd4100 /wd4510 /wd4355 /wd4127 mean, because I do not pay any attention to MSVC for three years, but they suppress superpedantic warnings that does not influence the result.

0

For the record, I like Job's answer, but I'm curious about a solution just using the variable name by itself in a "do-nothing" statement:

void foo(int x) {
    x; /* unused */
    ...
}

Sure, this has drawbacks; for instance, without the "unused" note it looks like a mistake rather than an intentional line of code.

The benefit is that no DEFINE is needed and it gets rid of the warning.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SO Stinks
  • 3,258
  • 4
  • 32
  • 37
  • 4
    I either used this with MSVC, but GCC raises "statement without effect" warning. So, Job's solution is the way to go. – Dmitrii Semikin May 14 '13 at 06:32
  • This approach still generates a warning in XCode – MOK9 Mar 05 '19 at 14:26
  • There isn't any one by the name "Job" here. What answer does it refer to? Can you link directly to it? Please respond by [editing (changing) your answer](https://stackoverflow.com/questions/3599160/how-can-i-suppress-unused-parameter-warnings-in-c/15608355#15608355), not here in comments (***without*** "Edit:", "Update:", or similar - the question/answer should appear as if it was written today). – Peter Mortensen Nov 14 '21 at 23:27
  • 1
    `(void) x; /* unused */` gets rid of the warning for me with GCC 9.3.0 – Peter Ludemann Dec 22 '21 at 20:41
0

I've seen this style being used:

if (when || who || format || data || len);
honk
  • 9,137
  • 11
  • 75
  • 83
Iustin
  • 1,220
  • 1
  • 12
  • 17
  • 15
    Hm. I cannot say I like this, as this assumes all parameters involved can be converted to a bool. – Suma Dec 21 '11 at 07:05
  • 1
    This isn't really a good convention, even though the compiler almost certainly will optimize it out, its not really clear whats going on and could confuse static source checkers. better use one of the other suggestions here IMHO. – ideasman42 Oct 31 '12 at 04:08
  • 1
    I can't believe I'm still getting replies to this. The question stated that it was for C. Yes, in another language this wouldn't work. – Iustin Mar 17 '13 at 14:11
  • 2
    I wouldn't use it but +1 for the novelty factor. – mgalgs Aug 20 '15 at 17:24
  • 4
    checking *truth* of variables can give warnings, for structs. eg. `struct { int a; } b = {1}; if (b);` GCC warns, `used struct type value where scalar is required`. – ideasman42 Sep 05 '15 at 08:16