繁体   English   中英

字符的预处理程序字符串化

[英]Preprocessor stringificaton of a character

是否可以在预处理器宏中将一个字符字符串化而不包含(')

例:

#define S(name, chr)  const char * name = #name chr

用法:

S(hello, 'W'); //should expand to 'const char * hello = "helloW"

谢谢一堆!,安德鲁

您不需要,因为在C中,相邻的字符串常量已合并。

即。

const char *hello = "hello" "W";

相当于

const char *hello = "helloW";

所以您当前的宏很好-像这样调用它:

S(hello, "W");

这是三种方式。 但是,没有人使用单引号的char:

#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;
};

所有输出:

你好

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM