#,##

Source: Internet
Author: User

#,##

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

Related Article

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.