Reputation: 59
#define ID proj1
#define PROJ ID##_data.h
As per my requirements definition of PROJ should have proj1_data.h
When I print PROJ It should give proj1_data.h ,
Please help me to get the desired result. Thanks in advance !
Upvotes: 0
Views: 171
Reputation: 70472
You can only print a string. So to print PROJ
, you would need to turn it into a string.
#define STRINGIZE(X) #X
#define STRINGIZE2(X) STRINGIZE(X)
STRINGIZE
applies the stringizing operator on the argument. It turns the argument into a string, essentially by surrounding it with quotation marks, and escaping the contents as necessary to create a valid string. STRINGIZE2
is used to allow the argument to be expanded by the preprocessor before it is turned into a string. This is useful when you want to stringize the expansion of a macro instead of the macro itself. For example STRINGIZE2(__LINE__)
will result in a string that represents the current line in the file, e.g. "1723"
. However, STRINGIZE(__LINE__)
results in the string "__LINE__"
.
Your definition of PROJ
faces a similar issue. It actually results in the token ID_data..h
rather than proj1_data.h
. You need a level of expansion indirection to allow ID
to expand before you concatenate.
#define PASTE(X, Y) X ## Y
#define MKFILE(X) PASTE(X, _data.h)
#define PROJ STINGIZE2(MKFILE(ID))
Using STRINGIZE2
allows the MKFILE
invocation to expand. Allowing MKFILE
to invoke PASTE
allows the ID
token to expand.
Upvotes: 2