Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 字符的预处理器字符串化_C++_C - Fatal编程技术网

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,它不会合并相邻的文本,是编译器合并的。