#defines with variable # arguments

Larry Yaffe lgy at pupthy2.PRINCETON.EDU
Thu May 12 05:01:09 AEST 1988


    Are there any versions of cpp which allow one to define macros which
accept variable numbers of arguments?  I keep wishing for this every time
I try to move code developed using sysV sprintf to a BSD system, so that
I could do something like:

#ifdef	BSD_TO_5_SPRINTF
char	*(*Sprintf)() = sprintf ;
#define sprintf(...)		strlen (Sprintf (...))
#endif

#ifdef	5_TO_BSD_SPRINTF
int	(*Sprintf)() = sprintf ;
#define sprintf(str,...)	((void) Sprintf (str,...), str)
#endif

    I know that some systems have `vsprintf' or some such thing which
can be called from a sprintf replacement routine, but since the vprintf
routines are not universally available (sadly), using them seems only
to add to portability problems.

    Does anyone know why the folks at Berkeley chose to have their
sprintf return its first argument, instead of the number of characters
printed?  I can't think of any good reason for this choice, since it
throws away valuable information (the # of characters printed) which
is painful to reacquire.

    On a separate topic: How common is the ability to initalize unions?
(In C, of course.)  Did this make it into the ANSI specs?

------------------------------------------------------------------------
Laurence G. Yaffe			lgy at pupthy.princeton.edu
Department of Physics			lgy at pucc.bitnet
Princeton University			...!princeton!pupthy!lgy
PO Box 708, Princeton NJ 08544		609-452-4371 or -4400



More information about the Comp.unix.wizards mailing list