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!