Variable length arg lists for macros

Brian Westley merlyn at rose3.rosemount.com
Tue Sep 6 12:34:53 AEST 1988


There is a way to do this, but it's ugly, dangerous,
noisy, and not portable...

short	_NARGS_;
#define	max(a,b,c,d,e) \
    (_NARGS_=5,-a-_NARGS_,-b-_NARGS_,-c-_NARGS_,-d-_NARGS_,-e-_NARGS_,\
    _max(_NARGS_,a-0,b-0,c-0,d-0,e-0))

main()
{
	int x,y;

	printf("%d ",max(3,5,4));
	x = max((-9),(-11),2*6-55);
	y = max(4,2-x,(-6),999,5-9);
	printf("%d %d %d\n",x,y,max(x,y-3));
}

_max(n,a,b,c,d,e)
int n;
int a,b,c,d,e;
{
	int result;

	result = a;
	if (--n>0 && b>result) result=b;
	if (--n>0 && c>result) result=c;
	if (--n>0 && d>result) result=d;
	if (--n>0 && e>result) result=e;
	return result;
}

----
Merlyn LeRoy



More information about the Comp.std.c mailing list