tags:

views:

145

answers:

4

Is there a way to concatenate keywords in a macro and get
C to behave in a more dynamic fashion as in:

#define macro(fun,ction,var,iable) function(variable)

I know this kind of thing exists in other languages.

+6  A: 

You can use ## to concatinate names in macros

fun##ction ...

Michael Dorgan
+2  A: 

No. Although there is ## as Michael says, it is applied (as all preprocessing) before C or C++ looks at keywords, and even using it to generate any preprocessing keyword is allowed to crash the preprocessor.

So, as a rule, if something doesn't compile when you input it directly, it won't when generated by the preprocessor either.

Potatoswatter
A: 

There are some caveats to its use (e.g. you've got to jump through some hoops to concatenate the results of other macro expansions), but the GCC docs discuss the basic form:

http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html

dash-tom-bang
A: 

http://en.wikipedia.org/wiki/C_preprocessor#Token_concatenation

advs89
You should add at least a short explanation and not just throw a link into the room.
Georg Fritzsche
It was my original intention to edit it to include an example but about four different people beat me to the chase so I didn't waste my time (I also didn't want to seem like I was copying their answer)
advs89