c语言##连接符号使用

1. ## 连接2个符号组成一个新的符号
#define A "hello "
#define B “world”
#define C(a,b) a##b
//testStr=“hello world”
const char testStr= C(A,B);
2. # 转字符串
#define TOSTRING(a) #a
#define A hello
#define B world
// p=“hello”
const char* p=TOSTRING(A);
#define C(a,b) TOSTRING(a) ## TOSTRING(b)
//testStr=“hello world”
const char testStr= C(A,B);

3. ##的坑
const char testStr= C(A,B);
(1.) vs下这种使用没问题的
(2.) gcc系列的编译器下,会编译不成功。
xcode提示:
Pasting formed … ,an invalid preprocessing token
网上查了一下,主要式c标准说明,用##操作后的结果必须是一个已经预定义过的符号。
vs和gcc对这个未预定义过的符号处理方式不一致原因。

预定义的符号包括
header names, identifiers, preprocessing numbers, character constants, string literals, punctuators, and single non-white-space characters that do not lexically match the other preprocessing token categories.

但是什么是预定义的符号,我也没弄明白。 string literals这个是否字符串属于呢

猜你喜欢

转载自blog.csdn.net/qq_16135205/article/details/86770372