C语言Printf重命名 封装Printf日志输出
1、安装Dev-C++或者是其它C编程工具
2、printf里的参数是可变长的,可边长的宏定义为"..."和"__VA_ARGS__",这两个在某些情况下是等效的。
关键代码:
#define D_DEBUG 0
#if D_DEBUG==1
#define d_debug(...) printf(__VA_ARGS__)
#else
#define d_debug(...)
#endif
3、我们只需要改变D_DEBUG的值,即可改变d_debug()是否有效,另外d_debug()的功能与printf的是一样的。
如:
#include <stdio.h>
//日志输出是否开启
#define D_DEBUG 1
//开启判断
#if D_DEBUG==1
#define d_debug(...) printf(__VA_ARGS__)
#else
#define d_debug(...)
#endif
//主函数
int main(){
//标准函数输出
printf("000000000000000000\r\n");
//日志输出
d_debug("1111111111111111111\r\n");
//返回值
return 0;
}
输出结果为:
4、在程序调试时D_DEBUG==1用于日志输出,在程序调试完成后可以D_DEBUG==0,关闭调试信息输出。
#include <stdio.h>
//日志输出是否开启
#define D_DEBUG 0
//开启判断
#if D_DEBUG==1
#define d_debug(...) printf(__VA_ARGS__)
#else
#define d_debug(...)
#endif
//主函数
int main(){
//标准函数输出
printf("000000000000000000\r\n");
//日志输出
d_debug("1111111111111111111\r\n");
//返回值
return 0;
}
输出结果为: