Is there a better way to get the value of a preprocessor variable?

Why does this work and why do we need 2 macros?

1
2
3
4
5
6
#define STR_HELPER(x)		#x
#define STR(x)				STR_HELPER(x)

#define AAA 500
#pragma message "content of AAA = " STR(AAA)   // prints content of AAA = 500
#pragma message "AAA = " STR_HELPER(AAA)       // prints AAA = AAA 


Why this "recursive call"?

Thanks,
Juan
Last edited on
Because macro arguments aren't expanded when they are "stringified", but they are (fully) expanded when they're used as an argument to another macro, per the argument prescan rules.

Edit:
To answer the title question, no, there is no better way -- that approach is idiomatic, and you must use at least 2 macros no matter what you do.
Last edited on
Topic archived. No new replies allowed.