Home > database >  How to concatenate literal strings with numerical macro?
How to concatenate literal strings with numerical macro?

Time:01-25

How to make a literal string by merging a non string macro as follow?

#define SOC 12

printf("This is the default SoC:" SOC "!");

[UPDATE]

This is embedded cpp 11 and I'd like to limit resource usage so I need a compile-time solution, not runtime.

CodePudding user response:

With preprocessor, you might stringify the value:

#define STRINGIFY(s) #s
#define STRINGIFY_VALUE(s) STRINGIFY(s)

and then use concatenation of C-strings:

printf("This is the default SoC:" STRINGIFY_VALUE(SOC) "!");

Demo

  • Related