调试技巧——C语言下BEBUG调试开关

1、一个参数打印宏定义

#define debugprintf(str)  printf(str)

  使用方法如下:.h文件下添加

#define _DEBUG 1
	
#if _DEBUG
#define debugprintf(str)  printf(str)
#else 	
#define debugprintf(str) 
#endif

2、多个打印参数宏定义

#define debugprintf(format,...)  printf(format, ##__VA_ARGS__)

  __VA_ARGS__ 是一个可变参数的宏,很少人知道这个宏,这个可变参数的宏是新的C99规范中新增的,目前似乎只有gcc支持(VC6.0的编译器不支持)。实现思想就是宏定义中参数列表的最后一个参数为省略号(也就是三个点),##__VA_ARGS__ 宏前面加上##的作用在于,当可变参数的个数为0时,这里的##起到把前面多余的","去掉的作用,否则会编译出错

使用方法如下:

#define _DEBUG 1
	
#if _DEBUG
#define debugprintf(format,...)  printf(format, ##__VA_ARGS__)
#else 	
#define debugprintf(format,...) 
#endif

3、编译器内置宏

__LINE__:在源代码中插入当前源代码行号;

__FILE__:在源文件中插入当前源文件名;

__DATE__:在源文件中插入当前的编译日期

__TIME__:在源文件中插入当前编译时间;

__STDC__:当要求程序严格遵循ANSI C标准时该标识被赋值为1;

__cplusplus:当编写C++程序时该标识符被定义。

  应用如下:

#define DEBUG(format,...) printf("FILE: "__FILE__", LINE: %d: "format"/n", __LINE__, ##__VA_ARGS__)

  

 

  

 

原文地址:https://www.cnblogs.com/syj888/p/11065015.html