C++ 字符的预处理器字符串化
是否可以在不包含(')的情况下对预处理器宏中的字符进行字符串化 例如:C++ 字符的预处理器字符串化,c++,c,C++,C,是否可以在不包含(')的情况下对预处理器宏中的字符进行字符串化 例如: #define S(name, chr) const char * name = #name chr 用法: S(hello, 'W'); //should expand to 'const char * hello = "helloW" 非常感谢!, Andrew您不需要这样做,因为在C中,相邻的字符串常量被合并 即 相当于 const char *hello = "helloW"; 因此,您当前的宏很好-只需这样
#define S(name, chr) const char * name = #name chr
用法:
S(hello, 'W'); //should expand to 'const char * hello = "helloW"
非常感谢!,
Andrew您不需要这样做,因为在C中,相邻的字符串常量被合并 即 相当于
const char *hello = "helloW";
因此,您当前的宏很好-只需这样称呼它:
S(hello, "W");
您不需要这样做,因为在C中,相邻的字符串常量被合并 即 相当于
const char *hello = "helloW";
因此,您当前的宏很好-只需这样称呼它:
S(hello, "W");
这里有三种方法。但是,没有一个使用单引号字符:
#include <iostream>
#define S1(x, y) (#x #y)
#define S2(x, y) (#x y)
#define S3(x, y) (x y)
int main(void)
{
std::cout << S1(hello, W) << std::endl;
std::cout << S2(hello, "W") << std::endl;
std::cout << S3("hello", "W") << std::endl;
};
#包括
#定义S1(x,y)(#x#y)
#定义S2(x,y)(#x y)
#定义S3(x,y)(x-y)
内部主(空)
{
std::cout这里有三种方法。不过,没有一种方法使用单引号字符:
#include <iostream>
#define S1(x, y) (#x #y)
#define S2(x, y) (#x y)
#define S3(x, y) (x y)
int main(void)
{
std::cout << S1(hello, W) << std::endl;
std::cout << S2(hello, "W") << std::endl;
std::cout << S3("hello", "W") << std::endl;
};
#包括
#定义S1(x,y)(#x#y)
#定义S2(x,y)(#x y)
#定义S3(x,y)(x-y)
内部主(空)
{
std::cout以及为什么要这样做?:|以及为什么要这样做?:|+1旁注-是C预处理器合并了相邻的字符串文本+1。@grdl:你错了。检查一下在这个宏上运行cpp,它不会合并相邻的文本,是编译器合并了相邻的字符串文本。+1旁注-是C预处理器合并了相邻的字符串文本rals+1。@grdl:你错了。请检查在这个宏上运行cpp,它不会合并相邻的文本,是编译器合并的。