C Macro Magic

Programming, for all ages and all languages.
Post Reply
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

C Macro Magic

Post 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.
Every good solution is obvious once you've found it.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C Macro Magic

Post 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.
Every good solution is obvious once you've found it.
Post Reply