Reputation:
I came across this line of code in legacy code:
#define func(x,y) if(strcmp(x,#y)==0)
Anyone have an idea of the purpose for the #
symbol preceding y
?
Upvotes: 0
Views: 129
Reputation: 9839
First of all you posted wrong or in complete code. #y
should be used with macro definition, not while using macro.
#define MAC(STR) #STR
int main(int argc, char* argv[])
{
printf(MAC(ME));//prints ME
printf(MAC("ME"));//prints "ME"
return 0;
}
Here I have defined MAC macro which takes one argument. I did it's stringification.
Also see second printf, it exactly prints string. So you need not to give pair of ""
.
Upvotes: 1
Reputation: 4756
as mentioned in the comments, this seems like stringification in a c macro.
here is a little example that uses your sample code:
#define doif(x, y) \
if(strcmp(x,#y)==0) { \
printf("doing! %s\n",x); \
}\
else { \
printf("not doing!\n"); \
}
int main()
{
char x[] = "test";
doif (x, test);
doif (x, something);
return 0;
}
the stringification operator actually pastes y
variable as a string before the compilation stage
Upvotes: 3