Variable length arg lists for macros

Wonderly gregg at ihlpb.ATT.COM
Fri Sep 2 23:57:20 AEST 1988


>From article <1036 at cbnews.ATT.COM>, by lvc at cbnews.ATT.COM (Lawrence V. Cipriani):
] In article <438 at ucsvc.unimelb.edu.au>, u5565522 at ucsvc.unimelb.edu.au (David Clunie) writes:
]
] ....
] 
]> 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
]> 
]> At present, this CANNOT BE DONE, without nesting parentheses (clumsy) or
]> using a call to an empty function (inefficient, unless you have inline
]> integration in an optimizing compiler).
] 
] Only clumsy for the uncoordinated :-)  You missed one more way:
] 
] 	#ifdef TRACE
] 	#define trace(anything)	anything
] 	#else
] 	#define trace(anything)
] 	#endif
] 
] and you use it like this:
] 
] 	c(i)
] 	{
] 		trace( if (i == 0) printf("boom\n");)
] 	}

I always use the following

#ifdef	DEBUG
#define	debug(x,y)	{if (x > debuglvl) printf y};
#else	DEBUG
#define	debug(x,y)
#endif	DEBUG

This gets you something like the above, although it does not allow
arbitrary values or conditions to govern the printing or execution
of debug information/code.  In general though I seldom need other
conditions to govern the debug output because I try to check all of
the conditions where things might go wrong anyway so I would write
the above as...

c(i)
{
	if (i == 0) {
		debug (1, ("OOOPS i == %d\n", i));
		abort();
	}
}

Gregg Wonderly
AT&T Bell Laboratories                   DOMAIN: gregg at ihlpb.att.com
IH2D217 - (312) 979-2794                 UUCP:   ihnp4!ihlpb!gregg

-- 
Gregg Wonderly
AT&T Bell Laboratories                   DOMAIN: gregg at ihlpb.att.com
IH2D217 - (312) 979-2794                 UUCP:   ihnp4!ihlpb!gregg



More information about the Comp.std.c mailing list