Variable length arg lists for macros
Kristian Wedberg
d85_kitte at tekn01.chalmers.se
Fri Sep 2 12:58:35 AEST 1988
In article <438 at ucsvc.unimelb.edu.au>, u5565522 at ucsvc.unimelb.edu.au (David Clunie) writes:
> How do people feel about the idea of preprocessor macros with variable
> length argument lists ?
>
> The concept has already been enshrined for true functions, allowing
> protoypes for things like the printf() family of functions, and a set of
> portable functions and macros to access the list of arguments.
>
> However this can not be done with macro calls. Wouldn't it be nice to be
> able to do something like ...
>
>
> #ifdef TRACE
>
> #define tracef(s,...) printf(s,...)
>
> #else
>
> #define tracef(s,...) /* nothing */
>
> #endif
>
...
> Regards ... David Clunie
My solution for the debugging-part of this is to use several macros.
The following is a (very) small extract from my debug-includefile:
#ifndef DEBUG_H
#define DEBUG_H
/* name: debug.h
*
* description: Header-file with debug-macros & prototypes.
*
pr(text) : print text (no " " or \n, & NO COMMAS!)
pr0("..\n") - pr6(...) : print string (with 0 - 6 arguments)
*
* problems:
* ??? No commas in pr() ???
*/
/* pr(), pr1() -> pr6()
* Produces no code when NPRINTING is defined! Just an empty statement
* from the semicolon.
*/
/* Print without arguments. "..." NOT needed */
#ifndef NPRINTING
#define pr(s) printf("s\n")
#else
#define pr(x)
#endif
/* Print with 0 to 6 arguments & "...\n" */
#ifndef NPRINTING
#define pr0(s) printf(s)
#define pr1(s,a1) printf(s,a1)
#define pr1(s,a1) printf(s,a1)
#define pr2(s,a1,a2) printf(s,a1,a2)
#define pr3(s,a1,a2,a3) printf(s,a1,a2,a3)
#define pr4(s,a1,a2,a3,a4) printf(s,a1,a2,a3,a4)
#define pr5(s,a1,a2,a3,a4,a5) printf(s,a1,a2,a3,a4,a5)
#define pr6(s,a1,a2,a3,a4,a5,a6) printf(s,a1,a2,a3,a4,a5,a6)
#else
#define pr0(x)
#define pr1(x,x1)
#define pr2(x,x1,x2)
#define pr3(x,x1,x2,x3)
#define pr4(x,x1,x2,x3,x4)
#define pr5(x,x1,x2,x3,x4,x5)
#define pr6(x,x1,x2,x3,x4,x5,x6)
#endif
Hope you'll find it useful...
Kristian Wedberg
More information about the Comp.std.c
mailing list