How to execute this function in C / C ++

I need a macro that helps to display the name and value of this parameter. This is something like the following code.

#define AA "Hello" #define BB "World" #define PRINT(input_param) printf("input_param: %s\n", (input_param)) void main() { PRINT(AA); PRINT(BB); } 

I expect the result: AA: Hello\n BB: World\n

But obviously this is not so. Can anyone fix me? Thanks.

+7
source share
1 answer

You need to associate the macro name with # . This is how assert() works:

 #define AA "Hello" #define BB "World" #define PRINT(input_param) printf(#input_param ": %s\n", (input_param)) void main() { PRINT(AA); PRINT(BB); } 

It might be clearer if I wrote it like this:

 #define PRINT(input_param) printf("%s: %s\n", #input_param, (input_param)) 
+12
source

All Articles