Variable length arg lists for macros

Peter Rowell peter at thirdi.UUCP
Fri Sep 2 05:04:26 AEST 1988


If the reason for variable arguments is to pass them to a routine
that takes varargs, the following horrible hack seems not only to
work but to be portable!

#define d_printf(level, stuff) ((level>=debug_level)? printf stuff :level=level)
                                   Note: NO parentheses --> ^     ^

This is then invoked by:

    d_printf(8, ("foo overflow: %d > %d", some_foo, max_foo));
		^ <----- Note the parentheses -----------> ^

By placing parentheses around the args for printf, they become 1 arg
from the point of view of the macro expander.  The reason for the ?: usage
is to allow this "if" anywhere a single expression could be without
screwing up things like:

    if (cond) d_printf(x,("bummer")); else somehthingelse;


----------------------------------------------------------------------
Peter Rowell				(You will need a bigger rock.)
Third Eye Software, Inc.		(415) 321-0967
Menlo Park, CA  94025			...!pyramid!thirdi!peter



More information about the Comp.std.c mailing list