C 宏中的#x 是什么意思? [英] What does #x inside a C macro mean?

查看:25
本文介绍了C 宏中的#x 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我有一个宏:

#define PRINT(int) printf(#int "%d
",int)

我有点知道结果是什么.但是#int 怎么会代表整个事情呢?

I kinda know what is the result. But how come #int repersent the whole thing?

我有点忘记了这个细节.有人可以给我一个提示吗?

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 ", 5); 这将打印 5 5;不是很有用;但是,如果您调用 PRINT(5+5) 宏扩展将是 printf("5+5" "%d ", 5+5); 这将print 5+5 10,没那么琐碎.

In this case, if you call PRINT(5) the macro expansion will be printf("5" "%d ", 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 ", 5+5); which will print 5+5 10, a little less trivial.

this tutorial on the C preprocessor 中解释了这个例子(顺便提一下, 是 c 宏磅符号的第一个 Google 搜索结果.

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 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆