新闻详情

新闻详情

首页 / 资讯中心 / 详情

【2017-05-26】【转】Stringification 在二级宏定义中的使用

发布时间:2026/9/12 19:10:04来源:尧图网络
【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 。至于采用二级宏定义的好处当然就是可以更加灵活的对宏展开时的内容进行控制。
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

半导体装备实时控制系统解析:鸿道RTOS如何保障确定性运动控制 2026/9/12 19:58:10

半导体装备实时控制系统解析:鸿道RTOS如何保障确定性运动控制

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

阅读更多 →
岩石CT图像裂缝分割实战:从数据增强到U-Net训练全流程 2026/9/12 19:58:10

岩石CT图像裂缝分割实战:从数据增强到U-Net训练全流程

简介:面向岩石裂缝与CT岩心裂缝语义分割任务的Python项目,基于深度学习方法提供完整源码与配套数据集,适合毕业设计、期末大作业、课程设计等场景,也方便新手通过注释清晰的代码快速上手。压缩包共11个文件,包含Python…

阅读更多 →
2024届本科 | C++ Linux服务端开发求职:从嵌入式到底层服务端的技术之路(附开源项目+简历) 2026/9/12 19:58:10

2024届本科 | C++ Linux服务端开发求职:从嵌入式到底层服务端的技术之路(附开源项目+简历)

2024届本科 | C Linux服务端开发求职:从嵌入式到底层服务端的技术之路(附开源项目简历) 关于我 你好,我是薛文军,2024届计算机科学与技术本科毕业,目前专注 Linux C 服务端/底层开发 方向求职,目…

阅读更多 →
3. Windows 远程桌面替代方案:节点小宝远程桌面功能深度评测,含异地组网与内网穿透使用教程 2026/9/12 19:58:10

3. Windows 远程桌面替代方案:节点小宝远程桌面功能深度评测,含异地组网与内网穿透使用教程

文章目录前言一、远程桌面功能更新解析1.1 底层协议切换:从系统 RDP 到自研传输协议1.2 P2P 点对点直连,非会员无速度与时长限制1.3 双屏独立分窗口1.4 隐私屏与权限控制二、技术视角:功能本质与适用边界2.1 "不依赖 RDP" 的技术本…

阅读更多 →
Linux文件(一).前传之基础IO 2026/9/12 19:58:10

Linux文件(一).前传之基础IO

一.理解文件1.狭义理解①.文件内容属性。②.文件储存在磁盘上。③.磁盘是永久性存储介质,因此⽂件在磁盘上的存储是永久性的。④.磁盘是外设(即是输出设备也是输⼊设备)。⑤. 磁盘上的⽂件本质是对⽂件的所有操作,都是对外设的输⼊…

阅读更多 →
遗传算法优化分位数回归双向LSTM预测模型解析 2026/9/12 19:55:10

遗传算法优化分位数回归双向LSTM预测模型解析

1. 项目概述:遗传算法优化分位数回归双向LSTM预测模型在时间序列预测领域,传统点预测方法往往难以捕捉数据分布的不确定性。我们提出的QRBILSTM-GA模型创新性地将分位数回归(Quantile Regression)与双向长短期记忆网络(Bidirectional LSTM)相结合&#x…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞