From: tstyles@my-dejanews.com
Newsgroups: comp.lang.c
Subject: Re: Macros with variable number of arguments?
Date: Mon, 22 Mar 1999 08:24:26 GMT
Message-ID: <7d4unk$2rc$1@nnrp1.dejanews.com>

In article <SouthSide-2003990057220001@bns.vip.best.com>,
SouthSide@kagi.com (Bob Bradley) wrote:
> Is it possible to create a macro with a variable number of arguments? I
> want to create a debugging macro that accepts a variable-number of
> arguments to call a printf-like routine, but when a preprocessor flag is
> off, I want it to completely eliminate the function call. For example:
>
> #if( DEBUG )
> #define DebugMacro( ... ) printf( ... )
> #else
> #define DebugMacro( ... )
> #endif

#if( DEBUG )
   #define  DebugMacro(Fmt_and_Args)    printf(Fmt_and_Args)
   #define  Arg(X)                      , (X)
#else
   #define  DebugMacro(Fmt_and_Args)
   #define  Arg(X)
#endif

Usage

   DebugMacro("Index %d, value %f"Arg(i)Arg(value));
This then allows you to insert 'hidden' arguments, eg
#if( DEBUG )
   #define  DebugMacro(Fmt_and_Args) fprintf(stderr,Fmt_and_Args)
or even,
#if( DEBUG )
   #define  DebugMacro(Fmt_and_Args) dbgprintf(__FILE__,__LINE__,Fmt_and_Args)
Tristan Styles #1485

Failure is not an Option
It is Standard Operating Procedure