Suppressing "ISO C99 requires rest arguments to be used"

David Mokon Bond picture David Mokon Bond · Nov 4, 2010 · Viewed 11.5k times · Source

Consider the following two macros:

#define PNORM( v, s, ... )  { \
  if( VERBOSITY_CHECK( v ) ) { \
    if( ( errno = pthread_mutex_lock(&server.output_mutex) ) ) { \
      PERROR_LOCKFREE( normal, "\tpthread_mutex_lock failed on output_mutex.\r\n" ) ; \
    } \
    fprintf( stdout, s, ## __VA_ARGS__ ) ; \
    fflush( stdout ) ; \
    if( ( errno = pthread_mutex_unlock(&server.output_mutex) ) ) { \
      PERROR_LOCKFREE( normal, "\tpthread_mutex_unlock failed on output_mutex.\r\n" ) ; \
    } \
  } \
}

#define PERROR_LOCKFREE( v, s, ... ) { \
  if( VERBOSITY_CHECK( v ) ) { \
    PERRNO ;\
    fprintf( stderr, s, ## __VA_ARGS__ ) ; \
    fflush( stderr ) ; \
  } \
}

Now consider an example use of these:

PNORM( verbose, "\tSomeText [%d] More [%p]\r\n", 0, ptr ) ;

When compiled with the -pedantic option and -std=c99 I get this error many times:

mycode.c:410:112: warning: ISO C99 requires rest arguments to be used

The complier is right in complaining about this but is there a simple way I can suppress this warning since I don't care about it?

Answer

Adam Rosenfield picture Adam Rosenfield · Nov 4, 2010

Combine the s argument with the variadic arguments so that you always have at least one argument as part of the ellipsis. This also allows you to avoid using the ,## extension of GCC:

#define PNORM( v, ... )  { \
  if( VERBOSITY_CHECK( v ) ) { \
    if( ( errno = pthread_mutex_lock(&server.output_mutex) ) ) { \
      PERROR_LOCKFREE( normal, "\tpthread_mutex_lock failed on output_mutex.\r\n" ) ; \
    } \
    fprintf( stdout, __VA_ARGS__ ) ; \
    fflush( stdout ) ; \
    if( ( errno = pthread_mutex_unlock(&server.output_mutex) ) ) { \
      PERROR_LOCKFREE( normal, "\tpthread_mutex_unlock failed on output_mutex.\r\n" ) ; \
    } \
  } \
}

#define PERROR_LOCKFREE( v, ... ) { \
  if( VERBOSITY_CHECK( v ) ) { \
    PERRNO ;\
    fprintf( stderr, __VA_ARGS__ ) ; \
    fflush( stderr ) ; \
  } \
}