Page 1 of 1

C Macro Magic

Posted: Wed Oct 20, 2004 3:06 am
by Solar
Harumph... I need help. :-\

Given is:

#define SUFFIX X

I need some macro C( n ) that, given that n is a numerical literal (e.g. 42), expands to that same literal with SUFFIX appended:

C( 42 )
-->
42X

I never got familiar with the # and ## operator of the C preprocessor, and I'm lost. Any help appreciated.

Re:C Macro Magic

Posted: Wed Oct 20, 2004 3:33 am
by Solar
Hm. Found out myself.

Code: Select all

#define SUFFIX X
#define app( x, y ) x ## y
#define append( x, y ) app( x, y )
#define C( value ) append ( value, SUFFIX )
You need a level of indirection, because "##" suppresses macro expansion of its arguments. >:(

Sorry for bothering.