#defines with variable # arguments
Guy Harris
guy at gorodish.Sun.COM
Thu May 12 11:12:23 AEST 1988
> Are there any versions of cpp which allow one to define macros which
> accept variable numbers of arguments?
None that I know of. There are tricks that can be used for this:
#define Sprintf(x) sprintf x
main()
{
char buf[40];
Sprintf((buf, "%d", 33));
Sprintf((buf, "%d %d", 33, 66));
}
but this requires the extra layer of parentheses; I presume you want a
"magic bullet" that requires few source changes to the program you're trying to
convert.
Neither K&R nor ANSI C have any syntax for macros of this sort.
> I keep wishing for this every time I try to move code developed using sysV
> sprintf to a BSD system,
Your best bet here is just to redo the code not to depend on the return value
of "sprintf". Sad, but true.
> Does anyone know why the folks at Berkeley chose to have their
> sprintf return its first argument, instead of the number of characters
> printed?
Nobody can possibly know that, because they weren't the ones who made that
decision. "sprintf" worked that way in V7 (although I don't think it was so
documented) - in fact, the System III SCCS code was written assuming this
behavior, which is kind of amusing since the System III "sprintf" returned the
number of characters generated.... (Some BSD/S5 differences are really V7/S5
differences; questions about why *those* differences exist should be directed
to AT&T, since they were responsible for both systems.)
More information about the Comp.unix.wizards
mailing list