问题描述
例如我有一个宏:
#define PRINT(int) printf(#int "%d\n",int)
我还挺知道是什么结果。
但如何来#int repersent整个事情?
I kinda know what is the result.But how come #int repersent the whole thing?
我有点忘了这个细节。有谁能够kindely给我一个提示?
I kinda forget this detail. Can anybody kindely give me a hint?
谢谢!
推荐答案
在此情况下(在宏定义应用于参数参考),英镑符号意味着这个参数扩展为传递参数的文字文本宏。
In this context (applied to a parameter reference in a macro definition), the pound sign means to expand this parameter to the literal text of the argument that was passed to the macro.
在这种情况下,如果调用 PRINT(5)
宏扩展将是的printf(5%d个\\ N, 5);
,它将打印 5
;不是非常有用;然而,如果你调用 PRINT(5 + 5)
宏扩展将的printf(5 + 5%d个\\ N5+ 5);
,它将打印 5 + 5 10
,少一点小事
In this case, if you call PRINT(5)
the macro expansion will be printf("5" "%d\n", 5);
which will print 5 5
; not very useful; however if you call PRINT(5+5)
the macro expansion will be printf("5+5" "%d\n", 5+5);
which will print 5+5 10
, a little less trivial.
这非常例子在本教程中(其中解释顺便说一下,第一个谷歌命中为)。
This very example is explained in this tutorial on the C preprocessor (which, incidentally, is the first Google hit for c macro pound sign).
这篇关于什么是一个C宏中#X是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!