C preprocessor: stringize macro and identity macro

后端 未结 2 1100
北海茫月
北海茫月 2020-12-16 05:01

I want to know the reason behind the output of this code. I couldn\'t come up with an answer.

#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
void main         


        
相关标签:
2条回答
  • 2020-12-16 05:09

    Just do the replacements.

    h(f(1, 2)) -> g(12) -> "12"
    
    g(f(1,2)) -> "f(1, 2)"
    

    You should also see here.

    0 讨论(0)
  • 2020-12-16 05:33
    h(f(1,2))
    

    f(1,2) is substituted for a. a is not the subject of a # or ## operator so it's expanded to 12. Now you have g(12) which expands to "12".

    g(f(1,2))
    

    f(1,2) is substituted for a. The # operator applied to a prevents macro expansion, so the result is literally "f(1,2)".

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