About # // # usage in macro definition () http://blog.chinaunix.net/space.php? Uid = 20792262 & Do = Blog & id = 201691 classification: algorithms and data structures
1st
In the macro definition of C/C ++, #/# has a special function. 1. # The operator converts a macro parameter to a string literal. It can only appear in the replacement list of macros with parameters.
View plaincopy to clipboardprint?
- # Include <cstdio>
- # Define print_str (s) printf (# s "= % s/n", S)
- Int main ()
- {
- Char * S1 = "AAA ";
- Print_str (S1 );
- Return 0;
- }
Output: S1 = aaa Note: printf (# s "= % s/n", S)-> printf (S1 "= % s/n", S1) -> printf ("S1 = % s/n", S1)-> printf ("S1 = % s/n", "AAA ") # S is only used to replace the macro parameter area. S is still a variable name. 2. # The operator can "stick" two marks (for example, identifiers) together to form a mark. (No need to be surprised. # The operator is called Mark bonding ".) If one of the operands is a macro parameter, "bonding" will occur after the formal parameter is replaced by the actual parameter.
View plaincopy to clipboardprint?
- # Include <cstdio>
- # Define print_str (I) printf ("str = % s/n", s # I)
- Int main ()
- {
- Char * S1 = "S1 ";
- Char * S2 = "S2 ";
- Char * S3 = "S3 ";
- Print_str (1 );
- Print_str (2 );
- Print_str (3 );
- Return 0;
- }
Output: Str = S1
STR = S2
STR = s33.c99 complex macro: Variable Parameter
View plaincopy to clipboardprint?
- # Include <cstdio>
- # Define print_str (format,...) do {printf (format, _ va_args _);} while (0)
- Int main ()
- {
- Char * str1 = "S1 ";
- Char * str2 = "S2 ";
- Print_str ("str1 is % S/nstr2 is % s/n", str1, str2 );
- Return 0;
- }
Output: str1 is S1
Str2 is s2but attention!
View plaincopy to clipboardprint?
- # Include <cstdio>
- # Define print_str (format,...) do {printf (format, _ va_args _);} while (0)
- Int main ()
- {
- Char * str1 = "S1 ";
- Char * str2 = "S2 ";
- Print_str ("str1 is over/N ");
- Return 0;
- }
Compile: macro1.cpp: In function 'int main ()':
Macro1.cpp: 8: Error: expected primary-expression before ') 'token
An error is prompted here because the variable parameter list is missing and only the format parameter is used. Here, a ',' is added after the format parameter ','. So, gnu cpp came up with a special # operation. That's right, # The operation is coming again, but it's not the above concatenation. If the variable parameter is ignored or empty, the "#" operation will remove the comma before the Preprocessor. If some variable parameters are provided during macro calls, gnu cpp will also work normally and put these variable parameters after the comma.
View plaincopy to clipboardprint?
- # Include <cstdio>
- # Define print_str (format,...) do {printf (format, ##__va_args _);} while (0)
- Int main ()
- {
- Char * str1 = "S1 ";
- Char * str2 = "S2 ";
- Print_str ("str1 is over/N ");
- Return 0;
- }
Output: str1 is over
2nd
In C/C ++, the macro definition is completed by define. There are three special symbols in define:
1. #: During macro expansion, the # parameter is replaced with a string, for example:
# Define P (exp) printf (# exp );
When P (asdfsadf) is called, # exp is replaced with "asdfsadf"
2. ##: splice the first and second words together. For example, in the C programming language:
# Define CAT (x, y) x # Y
Call CAT (VAR, 123) and expand to var123.
3. # @: change the value sequence to a character
# Define CH (c) # @ C
Call CH (A) and expand it to 'A '.
I wrote a short test program myself:
# Define a (a, B) a # B
# Define B (a) #
# Define C (a) # @
# Include <iostream>
Using namespace STD;
Void main ()
{
Int V = 0, V1 = 1, V12 = 2; // v stands for VAR
Cout <A (V1, 2) <Endl;
Cout <B (V1) <Endl;
Cout <C (v) <Endl;
}
Result:
1
V1
V
Copyright:Original works can be reprinted. During reprinting, you must mark the original source, author information, and this statement in hyperlink form. Otherwise, legal liability will be held.