C99 Macro to build a quoted string literal after evaluation -
I am developing an embedded application in C99, and there are some integer constants in this project, such as defined:
#define LEVEL1 0x0000 #define LEVEL2 (LEVEL1 + 1)
This has been useful since tracking these values for logging purposes, so I For example, to create a string from the evaluation versions of the above, use the macro:
strncpy (str, STRING (LEVEL2), Lane);
Ideally
will evaluate strncpy (str, "0x0001", LAN);
or even
strncpy (str, "0001", lane);
Using a two-step macro with # operator (as suggested) approximates its evaluation
strncpy (str, "( LEVEL1 + 1) ", Lane);
I would like to avoid using a run-time function - so my attempt on macro solutions. Suggestion:
Since the pre-processor stringer is a massive pain, you have to add a level of anger Both when creating and stringing the version number:
#define STRING1 (s) #s #define STRING (s) STRING1 #define LEVEL (x) x #define LEVEL1 LEVEL (1) Define # LEVEL2 LEVEL (2) printf (STRING (LEVEL2)); // 2
Comments
Post a Comment