#, # #和__VA_ARGS__

Source: Internet
Author: User
transferred from: Http://www.cnblogs.com/zhujudah/admin/EditPosts.aspx?opt=1The following macro definition, used in the Log printing, batch string dynamic combination, or very convenient.

1.#
If you want to include a macro parameter in a string, ANSI C allows you to do so, in the substitution section of the class function macro, #符号用作一个预处理运算符, it can convert the language symbol to the path string. For example, if x is a macro parameter, then #x can convert the name of the parameter to the corresponding string. The process is called a string (stringizing).
#incldue <stdio.h>
#define PSQR (x) printf ("The Square of" #x "is%d.\n", (x) * (x))
int main (void)
{
int y = 4;
Psqr (y);
PSQR (2+4);
return 0;
}
Output Result:
The square of Y is 16.
The square of 2+4 is 36.
The first time you call a macro, use "Y" instead of #x, and the second call with "2+4" Generation #x.
2.##
# #运算符可以用于类函数宏的替换部分. In addition, # #还可以用于类对象宏的替换部分. This operator combines two language symbols into a single language symbol. For example:
#define XNAME (n) x# #n
This is called by the macro:
XNAME (4)
After expansion:
X4
Program:
#include <stdio.h>
#define XNAME (n) x# #n
#define PXN (N) printf ("x" #n "=%d\n", x# #n)
int main (void)
{
int XNAME (1) =12;//int x1=12;
PXN (1);//printf ("x1 =%d\n", x1);
return 0;
}
Output Result:
X1=12
3. variable parameter macros ... And _ _va_args_ _
__VA_ARGS__ is a variable-parameter macro that few people know about this macro, which is the new C99 specification, and currently appears to be supported only by GCC (VC6.0 compiler does not support it).
The implementation idea is that the last parameter in the argument list in the macro definition is an ellipsis (that is, three points). This way the predefined macro _ _va_args_ _ can be used in the replacement section, replacing the string represented by the ellipsis. Like what:
#define PR (...) printf (__va_args__)
int main ()
{
int wt=1,sp=2;
PR ("hello\n");
PR ("weight =%d, shipping =%d", WT,SP);
return 0;
}
Output Result:
Hello
Weight = 1, shipping = 2
Ellipses can only replace the most later macro parameters.
#define W (x,..., y) error.

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.