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

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編譯環境下進行的。

 

posted @ 2016-07-07 22:32  xiulug  阅读(277)  评论(0编辑  收藏  举报