是否可以在预处理器宏中对字符进行字符串化,而不包括(')s
例:
#define S(name, chr) const char * name = #name chr
Run Code Online (Sandbox Code Playgroud)
用法:
S(hello, 'W'); //should expand to 'const char * hello = "helloW"
Run Code Online (Sandbox Code Playgroud)
谢谢你!安德鲁
你不需要,因为在C中相邻的字符串常量被合并.
即.
const char *hello = "hello" "W";
Run Code Online (Sandbox Code Playgroud)
相当于
const char *hello = "helloW";
Run Code Online (Sandbox Code Playgroud)
所以你当前的宏很好 - 只需这样调用它:
S(hello, "W");
Run Code Online (Sandbox Code Playgroud)
这有三种方式.但是,没有使用单引号字符:
#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;
};
Run Code Online (Sandbox Code Playgroud)
所有输出:
helloW