Passing Variable Numbers of Arguments

Tony Sanders sanders at peyote.cactus.org
Tue Feb 12 12:52:52 AEST 1991


In article <1991Feb11.225815.5875 at zoo.toronto.edu> henry at zoo.toronto.edu (Henry Spencer) writes:
>For a function, you'll need to use <stdarg.h> (ANSI C) or <varargs.h> (many
>old implementations).  Can't be done with a macro at all.
This is annoying.  How do get around this problem so I can easily
eliminate debug code if NODEBUG is set (or if DEBUG isn't set, whichever).
For instance...

This works great:
#ifdef NODEBUG
#define DPV(var,type) /* Removes code like magic */
#else
#define DPV(var,type) fprintf(stderr,"%s:%d, " # var " = %" # type "\n",var);
#endif

But I can't do this:
#ifdef NODEBUG
#define DP(fmt,...) /* sigh */
#else
#define DP(fmt,...) fprintf(stderr,fmt,...);
#endif

So I do this:

#ifdef NODEBUG
#define DP(level) if (0)	/* I hope the optimizer gets rid of this */
#else
extern DebugPrint(char *,...);
#define DP(level) if (debug&level) DebugPrint
#endif
DP(1)("this fmt string %s\n","sucks rocks");


Any better ideas???

-- sanders at peyote.cactus.org
First rule of software:  Throw the first one away.
and so on...
I am not an IBM representative and I speak only for myself.



More information about the Comp.lang.c mailing list