v1--Single-parameter macros
#define DRV_DEBUG 1#if DRV_DEBUG #define DRV_PRINT(x) printf(x)#else #define DRV_PRINT(x) #endif
This version of Drv_print (x) can only output a single variable--pure string
void foo(){ DRV_PRINT("Driver Initialize Success!");}
Change the Drv_debug macro definition when you do not need to print debug information
#define DRV_DEBUG 0
Of course, you can define it directly.
#define DRV_PRINT printf
But if the macro calls more than one parameter:
void foo(){ DRV_PRINT("Driver Initialize Success: ver %d.%d !", 1, 2);}
The compilation error in the Product code #define DRV_PRINT(x)
!
What to do? A way Virgo would never accept括号
void foo(){ DRV_PRINT(("Driver Initialize Success: ver %d.%d !", 1, 2));}
Whether it's debug code or product code, compile OK
v2--Specifying parameter macros
#define DRV_DEBUG 1#if DRV_DEBUG #define DRV_PRINT(fmt, val1, val2) printf(fmt, val1, val2)#else #define DRV_PRINT(fmt, val1, val2) #endif
If you only need to print a variable, the first 2
parameter is filled with random values, such as
void foo(){ DRV_PRINT("Driver Initialize Success: ver %d !", val1, 2);}
Similarly, if there are 4 parameters, it is:
void foo(){ DRV_PRINT("Driver Initialize Success: ver %d !", val1, 2, 3, 4);}
Silly, but there is no way: (, the VxWorks 5.5
kernel code is the way to do!)
v3--parameter number variable macro
Macros can be declared in C90 and C + + to accept a variable number of arguments, such as ARM编译器
:
#define DRV_DEBUG 1#if DRV_DEBUG #define DRV_PRINT(fmt, ...) printf(fmt, __VA_ARGS__)#else #define DRV_PRINT(fmt, ...) #endif
Now the DRV_PRINT
usage and printf
exactly the same, so cool features, C2000编译器
but not support!
aside, note that this feature C90 support, while C90 is a subset of C + +, but C99 and C + + are incompatible
from:1190000000456199
Custom print macro, two pairs of parentheses, single parameter macro, specified parameter macro, variable parameter macros