dag dag - 1 month ago 3x
C Question

Variadic UNUSED function/macro

A well-known and portable way to suppress C compiler warnings about unused variables is (see unused parameter warnings in C code):

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

I'm looking for a way to generalize this to take multiple inputs (of different type):

void foo(int a, long b, void* c){

/* Want this: */
ALL_UNUSED(a, b, c);

/* instead of: */

One way that seems to do the trick is to use a variadic function

static inline void ALL_UNUSED(int dummy, ...) {}

However, I suspect this solution is objectionable in the expert eye.

Is there a standard-compliant and portable (i.e. not using
) way to make a variadic UNUSED() function/macro? Many thanks!


There does not seem to exist a clean way of doing what I asked for in the context of C99 or the C preprocessor. Such is life.

In his answer below, @Dabo shows a pretty interesting way of doing what I asked for using a series of macros. This is neat and informative (at least to me), so I accept that answer. That said, I would not deploy it in a big project because it's tearse enough to outweigh the benefit it brings (in my eyes). But people will come to different conclusions here.

As noted below, the approach of using an empty variadic function is not perfect either. While it's a pretty elegant one-liner, it will provoke warnings about unititialized variables (if they are). Also, you have to trust your compiler to completely optimize it away, which I object to in principle but that all compilers I have tried with actually do.

One relevant case is when stubbing functions after an early high-level interface design phase. Then your unused variables will all be function arguments and initialized by definition, and the following approach works fine

static inline void UNUSED(int dummy, ...) {}

void foo(int a, long b, void* c){
UNUSED(a, b, b); /* No warnings */


Based on these two posts Variadic macro to count number of arguments, and Overloading macros i made the following

#define UNUSED1(x) (void)(x)
#define UNUSED2(x,y) (void)(x),(void)(y)
#define UNUSED3(x,y,z) (void)(x),(void)(y),(void)(z)
#define UNUSED4(a,x,y,z) (void)(a),(void)(x),(void)(y),(void)(z)
#define UNUSED5(a,b,x,y,z) (void)(a),(void)(b),(void)(x),(void)(y),(void)(z)

#define VA_NUM_ARGS_IMPL(_1,_2,_3,_4,_5, N,...) N
#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)

#define ALL_UNUSED_IMPL_(nargs) UNUSED ## nargs
#define ALL_UNUSED_IMPL(nargs) ALL_UNUSED_IMPL_(nargs)

what can be used as follows

 int main()
    int a,b,c;
    long f,d;


    return 0;

eclipse macro expansion gives :


compiled with gcc -Wall with no warnings


#define UNUSED1(z) (void)(z)
#define UNUSED2(y,z) UNUSED1(y),UNUSED1(z)
#define UNUSED3(x,y,z) UNUSED1(x),UNUSED2(y,z)
#define UNUSED4(b,x,y,z) UNUSED2(b,x),UNUSED2(y,z)
#define UNUSED5(a,b,x,y,z) UNUSED2(a,b),UNUSED3(x,y,z)


As for inline method you posted, a quick test

int a=0;
long f,d;


gives ‘f’ is used uninitialized in this function [-Wuninitialized] warning. So here at least one use case which breaks generality of this aproach