Token Pasting

Tony Sanders sanders at peyote.cactus.org
Tue Feb 12 11:25:58 AEST 1991


In article <1991Feb11.195439.17787 at csrd.uiuc.edu> bliss at sp64.csrd.uiuc.edu (Brian Bliss) writes:
> #define init(keyword)	keyword ## _symbol = \
>                           symbol_alloc ("keyword", keyword ## _type) 
should be:

#ifdef __STDC__
#define init(keyword)	keyword ## _symbol = \
                          symbol_alloc (# keyword, keyword ## _type) 
#else
#define init(keyword)	keyword ## _symbol = \
                          symbol_alloc ("keyword", keyword ## _type) 
#endif

Which I use like this:

#ifdef __STDC__
#define DPV(variable,type)   if (debug) fprintf(stderr, \
    "%s:%d, " # variable " = %" # type "\n",__FILE__,__LINE__,variable)
#else
#define DPV(variable,type)   if (debug) fprintf(stderr, \
    "%s:%d, variable = %type\n",__FILE__,__LINE__,variable)
#endif

It's quite ugly but it works great.  Your mileage may vary.
This works because # keyword converts to "expaned_keyword"
and "foo" "bar" "bat" "baz" converts to "foobatbatbaz".

-- sanders at peyote.cactus.org
First rule of software:  Throw the first one away.
and so on...
I am not an IBM representative and I speak only for myself.



More information about the Comp.lang.c mailing list