Macros with multi-line results?

Bj|rn P. Munch bjornmu at idt.unit.no
Fri Jan 11 13:27:15 AEST 1991


In article <16101 at burdvax.PRC.Unisys.COM>, fritzson at PRC.Unisys.COM (Richard Fritzson) writes:
|> I am trying to write a macro which generates more than one line of
|> output. Defining a macro such as
|> 
|> 	#define foo(a,b) body \
|> 	 more body \
|> 	 still more
|> 
|> and invoking it with
|> 
|> 	foo (one, two)
|> 
|> produces:
|> 
|> 	 body more body still more
|> 
|> as output from the C preprocessor. I would very much like to get
|> 
|> 	body
|> 	more body
|> 	still more
|> 
|> somehow. Is this possible? 

Yes, with a little help from tr.  I had to do the same thing once, and
my solution was to write the macro like this:

      #define foo(a,b) body @\
        more body @\
        still more

, and then process it like this:  (foo.xc is the source with the macro)

      cpp -P foo.xc | tr '@' '\012' > foo.c

Then you can compile foo.c.  In my case, I had a 100 line macro
expanding to function bodies with statements in embedded SQL.  I had
to do this trick because the SQL statements had to be at the start of
a line for the preprocessor.

Even worse, I needed to use macro arguments that contained commas, but
that problem could be solved in the same way.....  :-)

Your cpp may choke on the @ sign, in which case you can try a "legal"
C character like ?, | or ~.  Just make sure you don't need that
character for its "real" purpose anywhere else in the file.

Good luck!

-----
Bj|rn P. Munch               | Div. of Comp. Science & Telematics,
bjornmu at idt.unit.no          | Norwegian Institute of Technology (NTH),
PhD Student                  | Trondheim, Norway
 (some filler words here)    | You can finger me @jod.idt.unit.no



More information about the Comp.lang.c mailing list