#,##
Today, when I analyzed a source code, I encountered the following code:
[Cpp]
# Define CASE (x, y) case x: _ bsd_setlasterror (anApplInstance, BSD _ # y); break
# Define CASE (x, y) case x: _ bsd_setlasterror (anApplInstance, BSD _ # y); break
I think the # is very interesting, so I will sort out this knowledge point. # And # Are both pre-processing commands. Let's first look #.
# To convert a parameter to a string, see the following two examples.
Example 1:
[Cpp]
# Include <stdio. h>
# Include <iostream>
# Define P (A) printf ("% s: % d \ n", # A, );
Int main (int argc, char ** argv)
{
Int a = 1, B = 2;
P ();
P (B );
P (a + B );
System ("pause ");
}
# Include <stdio. h>
# Include <iostream>
# Define P (A) printf ("% s: % d \ n", # A, );
Int main (int argc, char ** argv)
{
Int a = 1, B = 2;
P ();
P (B );
P (a + B );
System ("pause ");
}
Example 2:
[Cpp]
# Define SQR (x) printf ("The square of x is % d. \ n", (x) * (x )));
# Define SQR (x) printf ("The square of x is % d. \ n ", (x) * (x); If you use macro: SQR (8 );
The output is: The square of x is 64.
Note that no. The character x in the quotation marks is treated as a plain text, rather than a language symbol that can be replaced.
If you really want to include macro parameters in a string, you can use "#" to convert the language symbol into a string. The preceding example is changed to the following:
[Cpp]
# Define SQR (x) printf ("The square of" # x "is % d. \ n", (x) * (x )));
# Define SQR (x) printf ("The square of" # x "is % d. \ n ", (x) * (x); then use: SQR (8 );
The output is: The square of 8 is 64.
Like the # operator, the # operator can be used to replace macro functions. This operator combines two language symbols into a single language symbol. Example:
[Cpp]
# Define XNAME (n) x # n
# Define XNAME (n) x # if n uses a macro like this: XNAME (8)
It is expanded as follows: x8
# It is a binder, which binds the first and second parts, that is, it indicates a "character. However, "#" cannot bond any character at will. It must be a valid C language identifier. In a single macro definition, a maximum