Commas in macro arguments

Tim McDaniel mcdaniel at amara.uucp
Wed Apr 11 05:46:14 AEST 1990


   esp_003 at jhunix.UUCP (Stdnt003) writes:
   >#define	call(a, b)	a ## b		/* (See K&R for details) */

("K&R" means 2nd Edition Kernighan and Richie)

Excuse me if this has already been pointed out, but it's my
understanding that this would be illegal for the purpose.

Page 230 of K&R2: "just after replacement of the parameters, each ##
is deleted, together with any white space on either side, so as to
concatenate the adjacent tokens and form a new token.  The effect is
undefined if invalid tokens are produced..."  The example on page 231
is more explicit:

       "#define	cat(x, y)     x ## y
... the call cat(cat(1,2),3) is undefined: the presence of ## prevents
the arguments of the outer call from being expanded.  Thus it produces
the token string
	cat	(	1	,	2	)3
and ')3' (the concatenation of the last token of the first argument
with the first token of the second) is not a legal token."

I conclude that
	call(foo,(a,b))
need not produce
	foo(a,b)
It conceptually first produces the token "foo(", which is not a legal
token.  (Of course, since the behavior is undefined, the compiler
could do what Stdnt003 expected---but it need not, and an integrated
preprocessor-compiler program probably won't.)

--
Tim McDaniel
Applied Dynamics International, Ann Arbor, MI
Internet: mcdaniel%amara.uucp at mailgw.cc.umich.edu
UUCP: {uunet,sharkey}!amara!mcdaniel



More information about the Comp.lang.c mailing list