#defines with variable # arguments
terryl at tekcrl.TEK.COM
terryl at tekcrl.TEK.COM
Sat May 14 03:25:44 AEST 1988
In article <2855 at phoenix.Princeton.EDU+ lgy at pupthy2.PRINCETON.EDU (Larry Yaffe) writes:
+
+ 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 came across this kludge years ago, hope it helps:
#define sprintf(prf) { Sprintf prf }
The TRICK is to ADD an extra level of parentheses, using it like thus:
sprintf( (buffer,"<what-ever-string-you-want>",<varargs>) );
This makes cpp THINK there is only one argument to the macro; I know you
don't want to muck with the sources and just want to put in the #define
and be done with it, but this is the only way I know of.
+ 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.
How painful is a strlen???? (I.E. I really don't understand your
complaint). Since Berkeley code is almost always derived upon ATT (or
whatever they're calling themselves this year!!! (-:) code, it sounds
like an earlier version of sprintf returned its first argument. However,
I also can't really think of any good reason for one return value over
the other.......
More information about the Comp.unix.wizards
mailing list