C语言 宏/macor/#define 高级技巧

1、在进行调试的时候,需要进行打印/PRINT,可以通过define进行自定义。例如,自己最常用的DEBUG_PRINT()

#define     DEBUG                                 1
//#undef DEBUG
#if DEBUG
#define DEBUG_PRINT(fmt, args...) fprintf(stdout, fmt"\tDEBUG: %s:%d:%s\n", ##args, __FILE__, __LINE__, __func__)
#else
#define DEBUG_PRINT(fmt, args...)
#endif

这是一个variable arguments的printf,可以像使用库函数一样使用DEBUG_PRINT()。

2、Pasting Tokens/粘贴token/将token和在...上

Each argument passed to a macro is a token, and sometimes it might be expedient to paste arguments together to form a new token. This could come in handy if you have a complicated structure and you'd like to debug your program by printing out different fields. Instead of writing out the whole structure each time, you might use a macro to pass in the field of the structure to print.

To paste tokens in a macro, use ## between the two things to paste together.

For instance

#define BUILD_FIELD(field) my_struct.inner_struct.union_a.##field

Now, when used with a particular field name, it will expand to something like

my_struct.inner_struct.union_a.field1

The tokens are literally pasted together.

3、String-izing Tokens/将token/标志/参数转化为字符串

Another potentially useful macro option is to turn a token into a string containing the literal text of the token. This might be useful for printing out the token. The syntax is simple--simply prefix the token with a pound sign (#).

#define PRINT_TOKEN(token) printf(#token " is %d", token)

For instance, PRINT_TOKEN(foo) would expand to

printf("<foo>" " is %d" <foo>)

(Note that in C, string literals next to each other are concatenated, so something like "token" " is " " this " will effectively become "token is this". This can be useful for formatting printf statements.)

For instance, you might use it to print the value of an expression as well as the expression itself (for debugging purposes).

PRINT_TOKEN(x + y);

参考:

1、The C Preprocessor

2、Preprocessor directives

最新文章

  1. 调用WebServices超时
  2. #iOS问题记录#关于NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9801)
  3. svg技术(可缩放矢量图形)介绍
  4. POJ 1016 模拟字符串
  5. [ACM_模拟] ZJUT 1155 爱乐大街的门牌号 (规律 长为n的含k个逆序数的最小字典序)
  6. 页面所有的button绑定同一个事件,点击不同的button赋值不同
  7. Android 自定义CheckBox 样式
  8. oracle时间加减的语句写法
  9. 手游 ui布局
  10. SmartCoder每日站立会议02
  11. 海量数据挖掘MMDS week3:流算法Stream Algorithms
  12. Go基础系列:函数(2)——回调函数和闭包
  13. $Django Rest Framework-频率组件,解析器
  14. scipy.stats
  15. Python内置类型(5)--迭代器类型
  16. 【微收藏】FourShadows.js – 时间感知的算法驱动的图标阴影JS库
  17. mac-Navicat Premium 12连接Oracle
  18. ssm框架结合axis2实例步骤
  19. 取得grid单元格里刚输入的文本,未保存的文本
  20. 【BZOJ3143】【HNOI2013】游走 高斯消元

热门文章

  1. Java多线程——Semaphore信号灯
  2. C++学习笔记(十二):类继承、虚函数、纯虚函数、抽象类和嵌套类
  3. JS 函数调用
  4. java.io.File中的pathSeparator与separator的区别
  5. iOS相机权限、相册权限、定位权限判断
  6. Mac下移动硬盘不能粘贴
  7. iOS开发——UI篇OC&amp;transform详解
  8. 关于Log4j的初始化
  9. stap-prep 需要安装那些内核符号
  10. MySQL的数据类型(转)