Question on ANSI ## pre-processor operator.
Stephen Vinoski
vinoski at apollo.HP.COM
Thu May 16 00:46:00 AEST 1991
In article <28302040.69ED at deneva.sdd.trw.com> knurlin at spf.trw.com (Scott Karlin) writes:
>I am looking for a clarification on the ANSI-C token merging
>operator (##). According to H&S: "After *all* [emphasis mine]
>macro replacements have been done, the two tokens surrounding
>any ## operator are combined into a single token." I interpret
>this to mean that:
>
>#define INDEX 0
>#define FN_NAME(x) name ## x
>void FN_NAME(INDEX) () { printf("Hello\n"); }
(I have cross-posted this to comp.std.c since a similar question
recently came up there. Follow-ups have been directed to
comp.lang.c.)
I had a problem with token pasting similar to yours. A compiler
writer said that the problem was caused by the fact that I was
attempting to paste arbitrary text with macro parameters; he said that
token pasting is only meant to work on macro parameters.
The ANSI C standard appears to agree with him; I believe it is section
3.8.3.3 (I don't have the standard handy) that says that ## causes
macro parameters to be pasted together. If I have misinterpreted the
standard, I'm sure I will hear about it. :-)
I have also noticed that K&R II only shows macro parameters being
pasted together.
What if you change your example to:
#define INDEX 0
#define REAL_FN_NAME(x,y) x ## y
#define FN_NAME(x) REAL_FN_NAME(name,x)
Then your example usage:
>void FN_NAME(INDEX) () { printf("Hello\n"); }
should produce the desired result.
-steve
| Steve Vinoski (508)256-0176 x5904 | Internet: vinoski at apollo.hp.com |
| HP Apollo Division, Chelmsford, MA 01824 | UUCP: ...!apollo!vinoski |
More information about the Comp.lang.c
mailing list