幾種方法實現C語言Macro for debug

本文介绍了两种在C语言中用于调试目的的宏定义方法。这两种方法均适用于GCC编译环境,通过预处理器指令实现条件编译,使得在开启调试模式时能够输出指定信息,方便开发者追踪程序运行状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 #define DEBUG 1
 5 #ifdef DEBUG
 6     #define DEBUG_PRINT(fmt, args...) fprintf(stdout, fmt, ##args) 
 7 #else
 8     #define DEBUG_PRINT(fmt, args...)
 9 #endif
10 
11 
12 void main()
13 {
14     DEBUG_PRINT("China. File:%s, Line:%d\n",__FILE__,__LINE__);
15 
16     return;
17 }

 

 參考:C #define macro for debug

2、

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <stdarg.h>
 4 
 5 #define DEBUG 1
 6 //#undef DEBUG
 7 
 8 #define DEBUG_PRINT( ...) \
 9     do {if (DEBUG) fprintf(stdout, ##__VA_ARGS__); } while(0)
10 
11 
12 void main()
13 {
14     DEBUG_PRINT("China.\n");
15 
16     return;
17 }

參考:C #define macro for debugging

 

這兩個均是在GCC編譯環境下進行的。

 

转载于:https://www.cnblogs.com/xuanyuanchen/p/5651822.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值