C language Variable parameter macro definition method __c language

Source: Internet
Author: User
Tags variadic

In GNU C, a macro can accept a variable number of arguments, just like a function, for example:

1 2 #define PR_DEBUG (Fmt,arg ...) \ printk (Kern_debug fmt, # #arg)

Passing a variable parameter table with a variable parameter macro (variadic macros)
You may be familiar with the use of variable parameter tables in a function, such as:

1 void printf (const char * format, ...);

Until recently, the variable parameter table can only be applied to real functions and cannot be used in macros.

The C99 compiler standard has finally changed this situation, allowing you to define variable parameter macros (variadic macros) so you can use macros that have parameter tables that can be changed. Variable parameter macros look like this:

1 #define DEBUG (...) printf (__va_args__)

The default number represents a parameter table that can be changed. Use the reserved name __va_args__ to pass parameters to the macro. When the call to the macro is expanded, the actual arguments are passed to printf (). For example:

1 Debug ("y =%d\n", y);


The processor replaces the invocation of the macro with the following:

1 printf ("y =%d\n", y);


Because Debug () is a variable parameter macro, you can pass a different number of arguments in each call:

1 Debug ("Test");  A parameter


Variable parameter macros are not officially supported by Ansi/iso C + +. Therefore, you should check your compiler to see if it supports this technology.

More easily print debugging information with GCC and C99 variable parameter macros

GCC preprocessing provides variable parameter macro definitions that are really handy:

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.