Expansion of macro arguments in ANSI cpp.

Alan Sparks sparks at suntan.viewlogic.com
Sat May 4 03:53:08 AEST 1991


In article <208 at tdatirv.UUCP>, sarima at tdatirv.UUCP (Stanley Friesen) writes:
|> In article <2610 at otc.otca.oz> grahamd at otc.otca.oz.au (Graham Dumpleton) writes:
|> <What is an ANSI conforming C preprocessor mean't to do in the following
|> <situation.
|> <
|> <#define preone onepre
|> <#define one two
|> <#define addpre(arg) pre##arg
|> <
|> <addpre(one)
|> 
|> I believe that the ANSI standard requires that macro arguments be expanded
|> before substitution into the macro body, and therefor before expansion of
|> the macro itself into the source.
|> 
|> Thus the answer is:
|> 
|> pretwo
|>  
|> <The preprocessor on a RS6000 running AIX ...
|>  
|> <onepre
|>  
|> I believe that this is *wrong* per the standard.
|> (I do not actually have a copy present with me, so I could be wrong).
|> -- 
|> ---------------
|> uunet!tdatirv!sarima				(Stanley Friesen)

Paragraphs in the Standard suggest that AIX is correct (section A12.3):

"... Unless the parameter in the replacement sequence is preceded by #,
or preceded or followed by ##, the argument tokens are examined for
macro calls and expanded as necessary, just before insertion."

".. Second, if the definition token sequence ... contains a ## operator,
then just after replacement of the parameters, each ## is deleted, together with any whitespace on either side, so as to concatenate the
adjacent tokens and form a new token."

"In both kinds of macro, the replacement token sequence is repeatedly
rescanned for more defined identifiers."

So, this suggests that 

	addpre(one)

turns into

	pre ## one

and (after concatenation),

	preone

and finally, after rescan, 

	onepre

-Alan
-- 

Alan Sparks      voice: (508) 480-0881  Internet: sparks at viewlogic.com
VIEWlogic Systems Inc., 293 Boston Post Rd. W., Marlboro MA 01752
Disclaimer: VIEWlogic didn't say this; I might have.  Who's asking?



More information about the Comp.lang.c mailing list