首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >C/C++宏串级联?

C/C++宏串级联?
EN

Stack Overflow用户
提问于 2018-02-08 01:00:09
回答 2查看 0关注 0票数 0
代码语言:txt
复制
#define STR1      "s"
#define STR2      "1"
#define STR3      STR1 ## STR2

是否有可能将STR 3==“S1”连接起来?

EN

Stack Overflow用户

发布于 2018-02-08 10:45:28

尝试如下:

代码语言:txt
复制
/*
 * Concatenate preprocessor tokens A and B without expanding macro definitions
 * (however, if invoked from a macro, macro arguments are expanded).
 */
#define PPCAT_NX(A, B) A ## B

/*
 * Concatenate preprocessor tokens A and B after macro-expanding them.
 */
#define PPCAT(A, B) PPCAT_NX(A, B)

宏:

代码语言:txt
复制
/*
 * Turn A into a string literal without expanding macro definitions
 * (however, if invoked from a macro, macro arguments are expanded).
 */
#define STRINGIZE_NX(A) #A

/*
 * Turn A into a string literal after macro-expanding it.
 */
#define STRINGIZE(A) STRINGIZE_NX(A)

然后,

代码语言:txt
复制
#define T1 s
#define T2 1
STRINGIZE(PPCAT(T1, T2)) // produces "s1"
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100003478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档