C语言Printf重命名 封装Printf日志输出

2025-10-20 22:42:29

1、安装Dev-C++或者是其它C编程工具

C语言Printf重命名 封装Printf日志输出

2、printf里的参数是可变长的,可边长的宏定义为"..."和"__VA_ARGS__",这两个在某些情况下是等效的。

关键代码:

#define D_DEBUG 0

#if D_DEBUG==1

   #define d_debug(...) printf(__VA_ARGS__) 

#else

   #define d_debug(...)

#endif

C语言Printf重命名 封装Printf日志输出

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;

输出结果为:

C语言Printf重命名 封装Printf日志输出

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;

输出结果为:

C语言Printf重命名 封装Printf日志输出

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢