### _ Va_args _ macro-related

Source: Internet
Author: User

1 .#
If you want to include a macro parameter in a string, ansi c allows this. In the macro replacement section of the class function, # symbol is used as a preprocessing operator, which can convert the language symbol to the string. For example, if X is a macro parameter, # X can convert the parameter name into a corresponding string. This process is called 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.
Use "Y" instead of # X for the first macro call; Use "2 + 4" generation # X for the second macro call.
2 .##
# Use a class FUNCTION macro to replace the operator. In addition, ## can be used to replace Class Object macros. This operator combines two language symbols into a single language symbol. For example:
# Define xname (n) x # N
Macro call:
Xname (4)
After expansion:
X4
Program:
# Include <stdio. h>
# Define xname (x) x #4
# 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;
}
3. Variable macros... and _ va_args __
The implementation idea is that the last parameter in the parameter list in the macro definition is the ellipsis (that is, three vertices ). In this way, the predefined macro _ va_args _ can be used in the replacement part to indicate what the ellipsis represents. For example:
# Define PR (...) printf (_ va_args __)
Pr ("hello"); --> printf ("hello ");
Pr ("Weight = % d, shipping = $. 2f", wt, SP );
--> Printf ("Weight = % d, shipping = $. 2f", wt, SP );
The ellipsis can only Replace the last macro parameter.
# 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.