C preprocessor tokenization does not expand macro?

后端 未结 2 1085

1) Why is the macro MSG not expanded in the following expression?

#define MSG Hello
#define HELLO(name)  MSG ## name

void HELLO(Dave) () {}
<
相关标签:
2条回答
  • 2021-01-04 21:41

    Because macro arguments are not substituted when preceded or followed by a ## operator.

    C11 §6.10.3.1 Argument substitution

    After the arguments for the invocation of a function-like macro have been identified, argument substitution takes place. A parameter in the replacement list, unless preceded by a # or ## preprocessing token or followed by a ## preprocessing token (see below), is replaced by the corresponding argument after all macros contained therein have been expanded. Before being substituted, each argument’s preprocessing tokens are completely macro replaced as if they formed the rest of the preprocessing file; no other preprocessing tokens are available.

    0 讨论(0)
  • 2021-01-04 21:57
    #define MSG Hello
    #define cat(x, y) x ## y
    #define cat2(x, y) cat(x, y)
    #define HELLO(name) cat2(MSG,name)
    

    Live demo @ ideone.

    0 讨论(0)
提交回复
热议问题