C宏创建的字符串 [英] C Macros to create strings
本文介绍了C宏创建的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
交替题名(帮助搜索)
- 转换一个preprocessor令牌字符串
- 如何从 C 宏的价值做出一个字符的字符串?
块引用>原始的问题
我想使用的 C
的#define
在编译时间来建立文字字符串。的字符串是用于调试改变域,释放等。
我想一些有的事情是这样的:
#IFDEF __TESTING
#定义IV_DOMAIN domain.org //在内部测试
#elif指令__LIVE_TESTING
#定义IV_DOMAIN test.domain.com //带电测试服务器
#其他
#定义IV_DOMAIN domain.com //生产
#万一//子域
#定义IV_SECUREsecure.IV_DOMAIN//secure.domain.org等
#定义IV_MOBILEm.IV_DOMAIN不过,preprocessor不评估范围内什么
- 有没有办法解决?
- 这甚至是一个好主意?
解决方案在C,字符串会自动连接在一起。例如,
为const char * S1 =富,酒吧;
为const char * S2 =foobar的;
S1
和S2
是相同的字符串。因此,对于您的问题,答案(无标记粘贴)是
#IFDEF __TESTING
#定义IV_DOMAINdomain.org
#elif指令__LIVE_TESTING
#定义IV_DOMAINtest.domain.com
#其他
#定义IV_DOMAINdomain.com
#万一#定义IV_SECURE安全。 IV_DOMAIN
#定义IV_MOBILEM。 IV_DOMAINAlternative Titles (to aid search)
- Convert a preprocessor token to a string
- How to make a char string from a C macro's value?
Original Question
I would like to use C
#define
to build literal strings at compile time.The string are domains that change for debug, release etc.
I would like to some some thing like this:
#ifdef __TESTING #define IV_DOMAIN domain.org //in house testing #elif __LIVE_TESTING #define IV_DOMAIN test.domain.com //live testing servers #else #define IV_DOMAIN domain.com //production #endif // Sub-Domain #define IV_SECURE "secure.IV_DOMAIN" //secure.domain.org etc #define IV_MOBILE "m.IV_DOMAIN"
But the preprocessor doesn't evaluate anything within ""
- Is there a way around this?
- Is this even a good idea?
解决方案In C, string literals are concatenated automatically. For example,
const char * s1 = "foo" "bar"; const char * s2 = "foobar";
s1
ands2
are the same string.So, for your problem, the answer (without token pasting) is
#ifdef __TESTING #define IV_DOMAIN "domain.org" #elif __LIVE_TESTING #define IV_DOMAIN "test.domain.com" #else #define IV_DOMAIN "domain.com" #endif #define IV_SECURE "secure." IV_DOMAIN #define IV_MOBILE "m." IV_DOMAIN
这篇关于C宏创建的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文