1

I have something like :

#define NBR 42
#define THE_ANS_IS theAnsIsNBR

Currently the second macro is expand as 'theAnsIsNBR' as expected, but i want it to be expand as 'theAnsIs42' i am not sure if it is even possible !?

Welgriv
  • 714
  • 9
  • 24
  • 2
    I think we need a canonical duplicate for the `##` operator, as this is quite a common FAQ. Anyone? – Lundin May 04 '18 at 13:14

2 Answers2

2
#define Paste(x, y)  x##y
#define Expand(x, y) Paste(x, y)

#define NBR 42
#define THE_ANS_IS Expand(theAnsIs, NBR)
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1
#define _CONCAT(x,y) x ## y
#define CONCAT(x,y) _CONCAT(x,y)

#define NBR 42
#define THE_ANS_IS CONCAT(theAnsIs, NBR)

This works because ## concatenates two tokens. The problem is, they aren't expanded first. But calling another macro on them expands them, therefore you need to nest two function-like macros here.