Question on ANSI ## pre-processor operator.

Scott Karlin knurlin at spf.trw.com
Wed May 15 03:37:03 AEST 1991


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"); }

Should expand to:

void name ## 0 () { printf("Hello\n"); }

And then:

void name0 () { printf("Hello\n"); }

Which is what I want.  However, two different ANSI (they say) compilers
are not defining a function called "name0".  Here is the actual code I
used:

/*
**   example.c
*/

#include <stdio.h>

#define PID 0

/*
**   Token merging macro...
*/
#define FN_NAME(x)  funct ## x

/*
**   Function prototypes...
*/
void main(void);
void funct0(void);


/*
**   Simple main function.  Nothing tricky here...
*/

void main()
{
   funct0();
   return;
}


/*
**   I believe that this should get expanded by the pre-processor to
**   funct0 ()   Note that I have intentionally placed a space between
**   the macro "call" and the empty function parameter list.
*/

void FN_NAME(PID) ()
{
   printf("Hello, world.\n");
   return;
}

Any information on the correct interpretation would be most welcome.

-- Scott Karlin                    Internet:  knurlin at spf.trw.com
-- TRW Data Systems Center         UUCP:      ...!uunet!knurlin at spf.trw.com
-- One Space Park, O2-1761         Phone: (213) 812-7335
-- Redondo Beach, CA 90278-1071    FAX:   (213) 812-8800



More information about the Comp.lang.c mailing list