1

I have a function func which expects a constant like PREFIX_XXXX as its argument. Also, there are macros like #define a AAAA. Is it possible to write a macro which, when called macro(a), would expand to func(PREFIX_AAAA)? I tried #define macro(x) func(PREFIX_##x), but of course this doesn't expand a to AAAA. Other ways I could think of didn't work either.

Jens
  • 69,818
  • 15
  • 125
  • 179
aplavin
  • 2,199
  • 5
  • 32
  • 53
  • @Barmar: but when adding `#define macro_(x) func(PREFIX_##x)` and `#define macro(x) macro_(x)` I have `error: pasting "PREFIX_" and "(" does not give a valid preprocessing token` at macro_. – aplavin Jan 26 '14 at 19:49
  • Sounds like you have a space in the macro definition like *#define macro (x)* ... instead of *#define macro(x) ...* – cup Jan 26 '14 at 20:18

1 Answers1

2

Arguments to the pasting ## do not undergo macro expansion. Therefore you will need another interstitial macro, e.g:

#define CONCAT(a,b) a ## b
#define MACRO(x) func(CONCAT(PREFIX_,x))

MACRO(TEST)

To test, run gcc -E on the above, and you get:

# 1 "macro.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "macro.c"

func(PREFIX_TEST)

Is that what you were after?

abligh
  • 24,573
  • 4
  • 47
  • 84