相关疑难解决方法(0)

宏中的#和##

  #include <stdio.h>
  #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(1,2)));
    return 0;
  }
Run Code Online (Sandbox Code Playgroud)

只需通过查看程序,"可能"期望输出,对于两个printf语句都是相同的.但是在运行程序时,你得到它:

bash$ ./a.out
12
f(1,2)
bash$
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

c stringification c-preprocessor

29
推荐指数
3
解决办法
2万
查看次数

#运算符在宏中做什么?

#include <stdio.h>

#define foo(x, y) #x #y

int main()
{
    printf("%s\n", foo(k, l));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:
kl

我知道##会连接.从输出看来,它似乎#也是连接.我对么?

如果我是正确的那么##运营商和#运营商之间有什么区别?

c macros

5
推荐指数
1
解决办法
187
查看次数

标签 统计

c ×2

c-preprocessor ×1

macros ×1

stringification ×1