【2017-05-26】【转】Stringification 在二级宏定义中的使用
[历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2017-05-26 标题【转】Stringification 在二级宏定义中的使用分类编程 / C C 标签宏·Stringification【转】Stringification 在二级宏定义中的使用本文转至【 https://my.oschina.net/moooofly/blog/126187 】最近在研究 Redis-2.6.12 的源码时重新对一处宏展开的用法进行了梳理记录如下在 zmalloc.h 中有如下定义/* Double expansion needed for stringification of macro values. */#define__xstr(s)__str(s)#define__str(s)#s在注释中出现了一个专有名词“stringification”经查阅网络上比较认可的翻译是“字符串化”。所以上面双重宏定义的用途为“用于宏值字符串化的双重宏展开 ”。这个概念似乎比较模糊无法让我深刻理解其用意幸好有下面的 资料 3.4Stringification Sometimes you may want to convert a macro argument into a string constant.Parameters are not replaced inside string constants,but you can use the ‘#’ preprocessing operator instead.When a macro parameter is used with a leading ‘#’,the preprocessor replaces it with the literal text of the actual argument,converted to a string constant.Unlike normal parameter replacement,the argument is not macro-expanded first.This is called stringification.There is no way to combine an argument with surrounding text and stringify it all together.Instead,you can write a series of adjacent string constants and stringified arguments.The preprocessor will replace the stringified arguments with string constants.The C compiler will then combine all the adjacent string constants into onelongstring.Here is an example of a macro definition that uses stringification:#defineWARN_IF(EXP)\do{if(EXP)\fprintf(stderr,Warning: #EXP );}\while(0)WARN_IF(x0);do{if(x0)fprintf(stderr,Warning: x 0 );}while(0);The argumentforEXP is substituted once,as-is,into theifstatement,and once,stringified,into the argument to fprintf.If x were a macro,it would be expanded in theifstatement,but not in the string.Thedoandwhile(0)are a kludge to make it possible to writeWARN_IF(arg);,which the resemblance of WARN_IF to a function would make C programmers want todo;see Swallowing the Semicolon.Stringification in C involves more than puttingdouble-quote characters around the fragment.The preprocessor backslash-escapes the quotes surrounding embedded string constants,and all backslashes within string and character constants,in order to get a valid C string constant with the proper contents.Thus,stringifying pfoo ;results inp \foo\ \;.However,backslashes that are not inside string or character constants are not duplicated:‘ ’ by itself stringifies to .All leading and trailing whitespace in text being stringified is ignored.Any sequence of whitespace in the middle of the text is converted to a single space in the stringified result.Comments are replaced by whitespacelongbefore stringification happens,so they never appear in stringified text.There is no way to convert a macro argument into a character constant.If you want to stringify the result of expansion of a macro argument,you have to use two levels of macros.#definexstr(s)str(s)#definestr(s)#s#definefoo4str(foo)fooxstr(foo)xstr(4)str(4)4s is stringified when it is used in str,so it is not macro-expanded first.But s is an ordinary argument to xstr,so it is completely macro-expanded before xstr itself isexpanded(see Argument Prescan).Therefore,by the time str gets to its argument,it has already been macro-expanded.内容有点长但确实是对 Stringification 作出了全面的解释。其中也对双重宏定义的用法进行了举例说明。针对 xstr(foo) 展开的情况由于 xstr() 本身是一个符合普通宏展开定义的东东而 foo 同样是这样的一个东东 所以在对 xstr(foo) 进行宏展开的时会按照正常的展开顺序进行即先展开 foo再展开 xstr 。至于采用二级宏定义的好处当然就是可以更加灵活的对宏展开时的内容进行控制。

相关新闻

Bun 运行时原理与工程实践:从开发体验到生产边界

Bun 运行时原理与工程实践:从开发体验到生产边界

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/26 11:22:08 阅读更多 →
AI模型部署四大方式:本地/服务器/无服务器/容器化实战指南

AI模型部署四大方式:本地/服务器/无服务器/容器化实战指南

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/26 11:22:13 阅读更多 →
【2017-03-09】Linux使用execve执行shell脚本或者其他程序

【2017-03-09】Linux使用execve执行shell脚本或者其他程序

[历史归档] 本文原发布于 cstriker1407.info 个人博客,内容为历史存档,仅供参考。 发布时间: 2017-03-09 | 标题:Linux使用execve执行shell脚本或者其他程序 | 分类: 编程 / C && C…

2026/10/26 11:22:13 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/11/2 0:25:10 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/11/1 10:36:53 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/11/1 10:36:51 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/11/2 1:16:51 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/11/1 10:35:11 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/11/1 9:35:41 阅读更多 →

日新闻

周新闻

月新闻