[comp.lang.c] ANSI Preprocessor Was: how widespread is this cpp bug?

aperez@bronco.uucp (Arturo Perez Ext.) (12/06/88)

From article <9026@smoke.BRL.MIL>, by gwyn@smoke.BRL.MIL (Doug Gwyn ):
> In article <6625@csli.STANFORD.EDU> wagner@arisia.xerox.com (Juergen Wagner) writes:
>>      proc/**/VERSION
>>People relying on this bug should change their habits. There are better ways
>>to concatenate tokens.
> 
> No, for Reiser-based preprocessors there aren't any better ways.
> ANSI-style token pasting is fairly new, and many C implementations
> in current use do not support it.


I don't really know anything about the ANSI view of things.  All I know
is what I glean from this news group (And I must admit, I am impressed by
the high level of knowledge exhibited). 

Well, the upshot of all this is:  how do ANSI compilers allow the concatenating
of preprocessor macro parameters?  Under pcc you can do things like

#define MAC_RO1(string1, string2) "strings1string2"

If you feed MAC_RO1 like so

	MAC_RO1(dir, file)

you get

	"dirfile"

Can you accomplish anything similar with ANSI C?  I guess what I'm
asking for is a preprocessor tutorial in respect to ANSI-C.

 

guy@auspex.UUCP (Guy Harris) (12/11/88)

 >#define MAC_RO1(string1, string2) "strings1string2"
 >
 >If you feed MAC_RO1 like so
 >
 >	MAC_RO1(dir, file)
 >
 >you get
 >
 >	"dirfile"
 >
 >Can you accomplish anything similar with ANSI C?

	#define	MAC_RO1(string1, string2) #string1 #string2

should do it, since:

	1) "#" preceding an occurrence of a formal argument in the body
	   of a macro turns it into a string

	2) adjacent string literals are concatenated

hence

	MAC_RO1(dir, file)

gets expanded to

	"dir" "file"

which gets turned into

	"dirfile"

>I guess what I'm asking for is a preprocessor tutorial in respect to
>ANSI-C.

A topic that's probably too big for a posting on comp.lang.c, although
somebody may post some further tutorial information.  You might check
out Kernighan & Ritchie, second edition, which is, to quote the blurb in
the upper right hand corner of the cover, "Based on Draft-Proposed ANSI
C" (BTW, "Draft-Proposed ANSI C" is often abbreviated as dpANS,
especially in this newsgroup).