博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编译器内置宏实现调试信息输出
阅读量:5076 次
发布时间:2019-06-12

本文共 3582 字,大约阅读时间需要 11 分钟。

C99中关于编译器内置宏的信息

6.10.8 Predefined macro namesThe rule that these macros may not be redefined or undefined reduces the complexity of the name space that the programmer and implementor must understand; and it recognizes that these macros have special built-in properties.The macros _ _DATE_ _ and _ _TIME_ _ were added in C89 to make available the time of translation. A particular format for the expansion of these macros was specified to aid in parsing strings initialized by them.The macros _ _LINE_ _ and _ _FILE_ _ were added in C89 to give programmers access to the source line number and file name.The macro _ _STDC_ _ allows for conditional translation on whether the translator claims to be standard-conforming. It is defined as having the value1. Future versions of the Standard could define it as 2, 3, etc., to allow for conditional compilation on which version of the Standard a translator conforms to. The C89 Committee felt that this macro would be of use in moving to a conforming implementation.The macro _ _STDC_VERSION_ _ was added in C95.A new feature of C99: C99 adds two additional predefined macros: _ _STDC_IEC_559_ _ and _ _STDC_IEC_559_COMPLEX_ _.

基本上,我们会使用到的有:

 

__DATE__ 编译日期

__TIME__ 编译时间

__FILE__ 编译文件路径

__LINE__ 当前源码所在行号

 

C99中关于不定参数宏定义的支持

A new feature of C99: C89 introduced a standard mechanism for defining functions withvariable numbers of arguments, but did not allow any way of writing macros with the sameproperty. For example, there is no way to write a macro that looks like a call to printf.This facility is now available. The macro definition uses an ellipsis in the same way to indicate avariable argument list. However, since macro substitution is textual rather than run-time, adifferent mechanism is used to indicate where to substitute the arguments: the identifier_ _VA_ARGS_ _. This is replaced by all the arguments that match the ellipsis, including thecommas between them.For example, the following macro gives a “debugging printf”:#ifdef DEBUG#define dfprintf(stream, ...) \fprintf(stream, "DEBUG: " _ _VA_ARGS_ _)#else#define dfprintf(stream, ...) ((stream, _ _VA_ARGS_ _, 0))#endif#define dprintf(...) dfprintf(stderr, _ _VA_ARGS_ _)For example,dprintf("X = %d\n", x);expands todfprintf(stderr, "X= %d\n", x);and thus to one offprintf(stderr, "DEBUG: " "X = %d\n", x);or((stderr, "X = %d\n", x, 0));If DEBUG is true, this calls fprintf, but first catenating "DEBUG: " to the format (whichmust therefore be a simple string). Otherwise it creates a comma expression (so that thearguments are still evaluated) with the value zero.There must be at least one argument to match the ellipsis. This requirement avoids the problemsthat occur when the trailing arguments are included in a list of arguments to another macro orfunction. For example, if dprintf had been defined as#define dprintf(format, ...) \dfprintf(stderr, format, _ _VA_ARGS_ _)and it were allowed for there to be only one argument, then there would be a trailing comma inthe expanded form. While some implementations have used various notations or conventions towork around this problem, the Committee felt it better to avoid the problem altogether.Similarly, the _ _VA_ARGS_ _ notation was preferred to other proposals for this syntax.

调试宏定义样例:

#include  #define __DEBUG__  #ifdef __DEBUG__  #define DEBUG(format,...) printf("File: "__FILE__", Line: %05d: "format"/n", __LINE__, ##__VA_ARGS__)  #else  #define DEBUG(format,...)  #endif

 

参考:http://www.cnblogs.com/lixiaohui-ambition/archive/2012/08/21/2649052.html

 

转载于:https://www.cnblogs.com/shijiezhenmei/p/3652145.html

你可能感兴趣的文章
Help Viewer 2.2 独立运行
查看>>
final关键字
查看>>
若瑟夫问题
查看>>
thinkphp3.2使用新版本mongodb
查看>>
表的垂直拆分和水平拆分
查看>>
软件工程课堂作业(三)——Right-BICEP软件单元测试
查看>>
逆波兰表达式
查看>>
etcd raft如何实现leadership transfer
查看>>
Spring Boot 2.x 系列教程:WebFlux 系列教程大纲(一)
查看>>
JavaScript 常用进度条
查看>>
jQuery学习-事件绑定
查看>>
让Mac OS X中的PHP支持mcrypt
查看>>
“校园知网”5月7日冲刺计划书
查看>>
代码书写格式
查看>>
Google 搜索知识
查看>>
Selenium 设置管理cookie,超时时间
查看>>
2. CNN卷积网络-前向传播算法
查看>>
nginx负载均衡和lvs负载均衡的比较分析
查看>>
Hadoop学习笔记(1)(转)
查看>>
C# 笔记 Func<TResult> 委托、Action<T> 委托
查看>>