0

I have a macro definition:

#define MY_PREFIX prefix_

Then I want to have definitions with concatenated suffices. I tried the following:

#define MY_NAME1 MY_PREFIXsuffix // Evaluates to 'MY_PREFIXsuffix'
#define MY_NAME2 MY_PREFIX ## suffix // Evaluates to 'MY_PREFIXsuffix'
#define MY_VAL(val) val // Intermediate macro
#define MY_NAME3 MY_VAL(MY_PREFIX)suffix // Evaluates to 'prefix_ suffix'
#define MY_NAME4 MY_VAL(MY_PREFIX) ## suffix // Evaluates to 'prefix_ suffix, preprocessor complains
#define CONCAT(a, b) a ## b 
#define MY_NAME5 CONCAT(MY_PREFIX, suffix) // Evaluates 'MY_PREFIXsuffix'

I thought that this should be simple. Any ideas?

The desired output should be that MY_NAME evaluates to 'prefix_suffix' (without the quotes)

3
  • Thanks for the answer. I tried it and it evaluates to 'MY_PREFIXsuffix'. I also updated the question with your suggestion. Commented Feb 22, 2017 at 16:28
  • sorry, I was looking for a dupe, and I left out the extra level of indirection which is needed. See the answer to the linked duplicate for an explanation.. #define CONCAT_(a,b) a##b #define CONCAT(a,b) CONCAT_(a,b) #define MY_NAME CONCAT(MY_PREFIX, suffix Commented Feb 22, 2017 at 16:33
  • Last suggestion worked indeed. Thanks. Commented Feb 22, 2017 at 16:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.