[英]# symbol in printf statement doesn't work
This code executes properly 此代码正确执行
#include<stdio.h>
#define JOIN(s1, s2) printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);
int main()
{
char *str1="India";
char *str2="BIX";
JOIN(str1, str2);
return 0;
}
but this code doesn't execute 但是此代码无法执行
#include<stdio.h>
int main()
{
char *str1="India";
char *str2="BIX";
printf("%s=%s %s=%s \n", #str1, str1, #str2, str2);
return 0;
}
I just replaced the first the macro of first segment coding .. but it doesn't work 我只是替换了第一段编码的第一个宏..但是它不起作用
Using the #var
feature to result in "var"
is part of the preprocessor, so you can only use that as part of a macro. 使用#var
功能生成"var"
是预处理器的一部分,因此您只能将其用作宏的一部分。
If you wanted to continue using it, often people write a macro called STRINGIFY: 如果您想继续使用它,通常人们会写一个名为STRINGIFY的宏:
#define STRINGIFY(x) #x
In your case though, the best thing would probably be to just do the quoting yourself. 但就您而言,最好的办法就是自己报价。
char *str1="India";
char *str2="BIX";
printf("%s=%s %s=%s \n", "str1", str1, "str2", str2);
This is preprocessor syntax, and can ONLY be used inside a macro definition ( #define..
). 这是预处理程序语法,只能在宏定义( #define..
)内部使用。
Your code is first run through a CPP, the C PreProcessor, which takes care of all #xxx..
syntax. 您的代码首先通过CPP(C #xxx..
程序)运行,该CPP处理所有#xxx..
语法。 The result of this is then passed to C compiler, which knows nothing about #xxxx..
syntax. 然后将其结果传递给C编译器,该编译器对#xxxx..
语法一无所知。
You can try it out yourself - instead of gcc , run cpp on your file and you can see the result of macro expansion. 您可以自己尝试-在文件上运行cpp而不是gcc ,您可以看到宏扩展的结果。
remove # it work only for macro 删除#它仅适用于宏
try this 尝试这个
int main()
{
char *str1="India";
char *str2="BIX";
printf("str1=%s str2=%s \n", str1, str2);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.