286

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

5
  • possible duplicate of What is the best way to supress "Unused variable x"-warning Commented Sep 17, 2014 at 7:14
  • 4
    @CiroSantilli This question has more upvotes, it would be better to mark the other question as duplicate. Commented Jan 20, 2015 at 13:18
  • 1
    See also the C++ version of this question Commented Jul 28, 2015 at 19:58
  • 2
    -Wno-unused-parameter, it's just too noisy and rarely catches bugs esp. when -Wshadow is used. Commented Aug 15, 2019 at 15:55
  • I'm surprised to hear /*...*/ around the parameter name doesn't work in C. I learned that trick before I made the transition from C to C++. Commented Jan 14, 2024 at 6:28

15 Answers 15

405

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);
    ...
}
Sign up to request clarification or add additional context in comments.

16 Comments

I just use (void)x directly
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.
@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.
I'm using #define UNUSED(...) (void)(__VA_ARGS__) which allows me to apply this to multiple variables.
@pasignature: I'd say to make your code self-explanatory. UNUSED(argsv) says what it does, (void)argsv does not in my opinion.
|
164

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

6 Comments

For any newbies like me, this means putting __attribute__ ((unused)) in front of the argument.
@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.
Also note that __attribute__((unused)) is a proprietary GCC extension. 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
Calling an extension within GCC "proprietary" is, uh, well it's something.
As a small generalization, __attribute__ ((unused)) int myUnusedFunc() gives no unused function warnings
|
78

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;.

5 Comments

Or maybe just #define __attribute__(x) for non-GCC environment (AFAIK none of the __attribute__ are supported by MSVC)?
That can work, but dunder prefixed terms are reserved for the compiler so I'd rather avoid this.
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
When putting it after I get warning: unused parameter ‘foo’ [-Wunused-parameter] (gcc 7.3.0)
UNREFERENCED_PARAMETER(p) is defined in WinNT.h
38

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.

2 Comments

its bad if you just want single parameter not whole file (even if you dont want change the code)
@Fox, this information is already contained in the answer, why duplicate?
25

With GCC with the unused attribute:

int foo (__attribute__((unused)) int bar) {
    return 0;
}

Comments

17

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;
}

1 Comment

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.
16

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

1 Comment

Clang supports these diagnostic pragmas as well clang.llvm.org/docs/…
8

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.

2 Comments

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.
Your problem is different, in any case. This question is about "unused parameter" warning while you got "unused variable" warning.
8

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

Comments

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.

1 Comment

You don't add any overhead in processing by using void(x), the compiler will optimize it out.
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.

Comments

1

I've seen this style being used:

if (when || who || format || data || len);

5 Comments

Hm. I cannot say I like this, as this assumes all parameters involved can be converted to a bool.
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.
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.
I wouldn't use it but +1 for the novelty factor.
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.
0

For compilers that complain about the accepted answer, the following alternative should work.

#define UNUSED(x) if (&(x) == 0) {}

Another alternative could be:

#define UNUSED(x) switch((long long)&(x)) { default: break; }

Comments

-1

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.

4 Comments

I either used this with MSVC, but GCC raises "statement without effect" warning. So, Job's solution is the way to go.
This approach still generates a warning in XCode
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, not here in comments (without "Edit:", "Update:", or similar - the question/answer should appear as if it was written today).
(void) x; /* unused */ gets rid of the warning for me with GCC 9.3.0
-1

when using the main function in c program and not using the main arguments you can void the arguments as below;

#include <stdio.h>
int main(int argc, char **argv)
{
    (void)**argv;

    printf("The program has %d arguments\n`enter code here`",argc);

    return (0);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.