Tell your compiler using a compiler specific nonstandard mechanism
See individual answers for __attribute__((unused))
, various #pragma
s 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.