Reputation: 61
I have two C macros, the first one is basically concatenating two tokens
#define _PY_CLASS_NAME(name) Py##name
The second macro is stringifying input argument
#define STR(text) #text
Because of the way C preprocessor work when I try something like
STR(_PY_CLASS_NAME(name))
I actually get "_PY_CLASS_NAME(name)". So the question is, how to avoid it?
I tried something like
#define CONCAT(A, B) #A###B
and it works. But maybe it is a better way to do it?
Upvotes: 0
Views: 297
Reputation: 30926
#define _PY_CLASS_NAME(name) Py##name
#define STR(a) STR_(a)
#define STR_(a) #a
This solves the problem in a different way and also would clarify how it macro works. Reason is - when macro arguments are substituted in the macro body, they are expanded until they appear with the #
or ##
pre-processor operators in that macro.
Now doing this printf("%s\n",STR(_PY_CLASS_NAME(name)));
prints Pyname
.
Edit: The second one you mentioned won't work. The compiler complains as mentioned, of absence of valid preprocessing token.
Upvotes: 1