Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!bloom-beacon!think!ames!amdahl!sri-unix!quintus!ok From: ok@quintus.UUCP (Richard A. O'Keefe) Newsgroups: comp.unix.wizards Subject: Re: #defines with variable # arguments Message-ID: <979@cresswell.quintus.UUCP> Date: 15 May 88 08:53:57 GMT References: <2855@phoenix.Princeton.EDU+ <2633@tekcrl.TEK.COM> <2886@phoenix.Princeton.EDU> Distribution: na Organization: Quintus Computer Systems, Mountain View, CA Lines: 26 In article <2886@phoenix.Princeton.EDU>, lgy@pupthy2.PRINCETON.EDU (Larry Yaffe) writes: > In article <2633@tekcrl.TEK.COM> terryl@tekcrl.tek.com writes: > >In article <2855@phoenix.Princeton.EDU+ (lgy@pupthy2.PRINCETON.EDU) I wrote: > >+ Are there any versions of cpp which allow one to define macros which > >+accept variable numbers of arguments?> > I came across this kludge years ago, hope it helps: > > The TRICK is to ADD an extra level of parentheses, using it like thus: > > sprintf( (buffer,"",) ); > >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 > A number of people suggested variants of the above. Just a wild heretical suggestion, but there is *another* macro processor which comes standard with UNIX. System V has it and BSD has it, and there is a rather nice public domain version. I refer, of course, to m4. What I suggest is a one-time conversion: sprintf(S,F) -> sprintf0(S,F) sprintf(S,F,A) -> sprintf1(S,F,A) ... sprintf(S,F,A,B,C,D,E,F)-> sprintf6(S,F,A,B,C,D,E,F) however many you need. This is easy enough to do with m4. Then you write your cpp macros for each sprintfN, which is a bit of a pain, but means you don't have to keep using m4. It is precisely because m4 is there to fall back on when you need REAL macro-power that cpp can be so weak.