本文介绍了çpreprocessor:stringize宏观和身份宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这个code的输出背后的原因。我不能想出一个答案。

 的#define F(A,B)一## b
的#define克(一)#a的
的#define小时(一)克(一)
无效的主要()
{
   的printf(%s%S,H(F(1,2)),G(F(1,2)));
}

PS:输出 12°F(1,2)。我认为这是 12 12 F(1,2)F(1,2)


解决方案

  H(F(1,2))

F(1,2)代替了 A A 不是这个问题的 ## 运营商因此它扩大到 12 。现在你有克(12)它扩展成12

 克(F(1,2))

F(1,2)代替了 A 。在运营商应用到 A prevents宏扩展,所以结果是字面上 F(1,2)

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()
{
   printf("%s %s",h(f(1,2)),g(f(1,2)));
}

PS: output is 12 f(1,2). I thought it was 12 12 or f(1,2) f(1,2).

解决方案
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)".

这篇关于çpreprocessor:stringize宏观和身份宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 03:12