C Macros to create strings

后端 未结 7 2133
佛祖请我去吃肉
佛祖请我去吃肉 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:13

    What you need are the # and ## operators, and automatic string concatenation.

    The # preprocessing operator turns the macro parameter into a string. The ## operator pastes two tokens (such as macro parameters) together.

    The possibility that comes to mind to me is

    #define IV_DOMAIN domain.org
    #define IV_SECURE(DOMAIN) "secure." #DOMAIN
    

    which should change IV_SECURE to

    #define IV_SECURE "secure." "domain.org"
    

    which will automatically concatenate to "secure.domain.org" (assuming the phases of translation are the same in C as C++).

    ANOTHER EDIT: Please, please read the comments, which show how I've managed to get confused. Bear in mind that I am thoroughly experienced in C, although perhaps a touch rusty. I would delete this answer, but I thought I'd leave it as an example of how easy it is to get confused by the C preprocessor.

提交回复
热议问题