C Macros to create strings

后端 未结 7 2131
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 04:37

Alternative Titles (to aid search)

  • Convert a preprocessor token to a string
  • How to make a char string from a C
7条回答
  •  庸人自扰
    2020-11-30 05:23

    In C, string literals are concatenated automatically. For example,

    const char * s1 = "foo" "bar";
    const char * s2 = "foobar";
    

    s1 and s2 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
    

提交回复
热议问题