Variable parameters for C + + macro definitions detailed parsing _c language

Source: Internet
Author: User

In the process of writing code, often output some debugging information to the screen, will normally call printf this kind of function.
But when debugging is done, we need to manually remove or annotate these places.
Recently watching the "Linux C programming One-stop learning" This book, I think of a method:

Copy Code code as follows:

void myprintf (char* fmt, ...)
{
}
#ifdef DEBUG
#define printf (fmt, args ...) myprintf (FMT, # #args)
#endif

Debug phase with Debug debugging, the official line can turn printf into an empty function.
One potential risk of doing this is that it may cause dictation glib function to call printf output error log also to be canceled.
Thankfully, most glib calls should be fprintf.

Although the problem solved, but I to args ... and # #args还是不太了解. On the internet to find some of the GCC handbook information as follows:
Macros with variable parameters (Macros with a Variable number of Arguments)
In the 1999 version of the ISO C standard, macros can be defined as functions with variable parameters. The syntax of a macro is similar to the syntax of a function.
Here's an example:
Copy Code code as follows:

#define DEBUG (format, ...) fprintf (stderr, format, __va_args__)

Here, ' ... ' means variable parameters. When this type of macro is invoked, it (here refers to ' ... ') is represented as 0 or more symbols, including the comma inside, until the end of the right bracket. When invoked, the collection of symbolic sequences in the macro body (macro) replaces the inside __va_args__ identifier. For more information, refer to the CPP manual.

GCC always supports complex macros, which use a different syntax so that you can give variable parameters a name, just like any other parameter. For example, the following example:
Copy Code code as follows:

#define DEBUG (format, args ...) fprintf (stderr, format, args)

This is exactly the same as the macro example defined in the ISO C above, but it is more readable and easier to describe.
GNU CPP also has two more complex macro extensions that support the format defined in both of the above formats.
In standard C, you can't omit a variable parameter, but you can pass it an empty argument. For example, the following macro call is illegal in ISO C because there is no comma after the string:
Copy Code code as follows:

Debug ("A message")

GNU CPP In this case allows you to completely ignore the variable parameters. In the example above, the compiler still has a problem (complain), because after the macro is expanded, there is an extra comma behind the string.
To solve this problem, CPP uses a special ' # # ' operation.
The writing format is:
Copy Code code as follows:

#define DEBUG (format, ...) fprintf (stderr, format, # # __va_args__)

Here, if the variable parameter is ignored or empty, the ' # # ' operation causes the preprocessor (preprocessor) to remove the comma in front of it. If you do provide some variable parameters when a macro is invoked, the GNU CPP works fine, and it puts these variable parameters behind the comma. Like other pasted macro parameters, these parameters are not extensions of macros.

specific See "Linux C Programming One-stop learning", by the way praise this book, write very well!

Related Article

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.