Custom print macro, two pairs of parentheses, single parameter macro, specified parameter macro, variable parameter macros

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.