) Pre-processing definition of C/C ++ stringizing operator (#) charizing operator (# @) token-pasting operator (##)

Source: Internet
Author: User
Definition of pre-processing for C/C ++:
1. stringizing operator (#)

In C and C ++, the number sign # is given a new meaning, that is, the string operator. The function is to convert the input parameter name in the macro definition into a pair of double quotation marks.
Parameter Name string. It can only be used in macro definitions with input parameters, and must be placed before the parameter name in the macro definition body.
For example:
# Define example (instr) printf ("the input string is: \ t % s \ n", # instr)
# Define example1 (instr) # instr
When the macro definition is used:
Example (ABC); during compilation, It will be expanded to: printf ("the input string is: \ t % s \ n", "ABC ");
String STR = example1 (ABC); it will be expanded to: String STR = "ABC ";

Note:
1. Processing space
A. Ignore the spaces before and after the input parameter name.
For example, STR = example1 (ABC); will be extended to STR = "ABC ";
B. When there is a space between input parameter names, the compiler will automatically connect each sub-string and use only one space in each sub-string to connect, ignoring the additional space.
For example, STR = exapme (abc def); it will be extended to STR = "ABC Def ";

2. Escape characters
A. In some forms of input parameter names, if special characters exist, the compiler will automatically add escape characters '\' to them '\'.
For example:
String STR = example1 ("ESCAP '\ e"); equivalent to: Str = "\" ESCAP \' \ e \"";
B. vc6.0 and vc7.0 cannot correctly parse all situations that require special characters. In this case, the error report: Error C2001: the constant contains a line break.
For example:
Example1 (ABC \ '); // here the alarm error C2001: the constant contains a line break.

Ii. charizing operator (#@)

Character operators. It can only be used in macro definitions with input parameters, and must be placed before the parameter name in the macro definition body. To convert the name of a single-character parameter to a character and enclose it with a single reference.
For example:
# Define examplechar (inchar) # @ inchar
Use this macro definition:
Char A = examplechar (a); will be extended to: Char A = 'B ';

Note:
In default type conversion in vc6.0 and vc7.0, int can be truncated to Char. Therefore, the parameter name cannot exceed 4 characters.
For example:
Char A = example (ABCD) is truncated to a = 'D '. At the same time, the compiler will provide: Warning c4305: "=": Truncated from "int" to "char"

3. Token-pasting operator (##)

#: Symbolic join operator.
Macro definition: The parameter name, that is, the form parameter, for example, # define sum (a, B) (a + B); In A and B are representative symbols of a parameter, that is, the form parameter.
## Is used to convert multiple form parameters defined by a macro into one actual parameter name.
For example:
# Define examplenum (n) num # N
Int num9 = 9;
Usage:
Int num = examplenum (9); it will be extended to int num = num9;

Note:
1. When ## is used to connect a form parameter, the spaces before and after ## are optional.
For example: # define examplenum (n) num # N is equivalent to # define examplenum (n) num # N
2. The actual parameter name after connection must be an existing parameter name or a macro defined by the compiler.

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.