# and ## in macros

前端 未结 3 2042
挽巷
挽巷 2020-11-27 03:20
  #include 
  #define f(a,b) a##b
  #define g(a)   #a
  #define h(a) g(a)

  int main()
  {
    printf(\"%s\\n\",h(f(1,2)));
    printf(\"%s\\n\",g(f(         


        
3条回答
  •  遥遥无期
    2020-11-27 03:49

    Because that is how the preprocessor works.

    A single '#' will create a string from the given argument, regardless of what that argument contains, while the double '##' will create a new token by concatenating the arguments.

    Try looking at the preprocessed output (for instance with gcc -E) if you want to understand better how the macros are evaluated.

提交回复
热议问题