C 预处理器:字符串化宏和标识宏 [英] C preprocessor: stringize macro and identity macro

查看:13
本文介绍了C 预处理器:字符串化宏和标识宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这段代码输出背后的原因.我想不出答案.

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:输出是 12 f(1,2).我以为是 12 12f(1,2) 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) 代替 a.a 不是 ### 运算符的主题,因此它被扩展为 12.现在你有 g(12) 扩展为 "12".

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) 代替 a.应用于 a# 运算符可防止宏扩展,因此结果字面上为 "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)".

这篇关于C 预处理器:字符串化宏和标识宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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