使用宏连接整数

concatenation of integers using macro

只需在 forum 上阅读这篇文章。有人可以解释一下吗,为什么以及如何工作?

#include <stdio.h>
#define merge(a, b) b##a
int main(void)
{
    printf("%d ", merge(12, 36));
    return 0;
}

另一方面,如果我们不使用宏,那么编译器会给出编译错误。

## 是 MACRO 替换的一种形式。引用 C11,第 6.10.3.3 章,## 运算符

If, in the replacement list of a function-like macro, a parameter is immediately preceded or followed by a ## preprocessing token, the parameter is replaced by the corresponding argument’s preprocessing token sequence;

因此,在您的情况下,根据 MA​​CRO 定义,

#define merge(a, b) b##a

在您的代码中

merge(12, 36)

看起来像

3612

在预处理阶段之后。

FWIW,完整的语句,printf("%d ", 3612);,就这么简单。