#, #-Conversion from csdn in macro definition

Source: Internet
Author: User

1,Use a # Before a parameter in a Preprocessor macro. The Preprocessor converts this parameter to a character array. (Original article: when you put a # Before an argument in a Preprocessor

Macro, The Preprocessor turns that argument into a character array. This,

Combined with the fact that character arrays with no intervening punctuation are concatenated into a single character array, allows you to make a very convenient macro for printing the values of variables during debugging)

# Include "iostream"

Using namespace STD;

# Define P (a) cout <# A <":" <(a) <Endl;

Int main ()

{

Int A = 1, B = 2;

P ();

P (B );

P (A + B );

Return 1;

}

Http://blog.163.com/zhoumhan_0351/blog/static/39954227201032124942513/

2. # define D (a) cout <# A "= [" <A <"]" <Endl;

3 ,#It means "stringized. In the macro definition, # converts the following parameters into a string.

For example:
> # Define Foo (ARG) My # ARG
Then
> Foo (ABC)
Equivalent to myabc

For example:
> # Define strcpy (DST, Src) strcpy (DST, # SRC)
Then
> Strcpy (buff, ABC)
Equivalent to strcpy (buff, "ABC ")

In addition, if the parameter # Is a macro, ## will prevent the macro from being expanded,That is, only replace once.

# Define strcpy (a, B) strcpy (A ##_ P, # B)

Int main ()

{

Char var1_p [20];

Char var2_p [30];

/* Note here */

Strcpy (var1, var2), var2 );

/* Is it going to be: strcpy (var1_p, "var2") _ p, "var2")?

* The answer is no:

* The expanded result is strcpy (var1, var2) _ p, "var2 ")

* ## The macro expansion of parameters is blocked!

* If the # And # are not used in the macro definition, the macro will be fully expanded.

*/

}

Http://blog.chinaunix.net/u/17855/showart_113663.html

4,About # In common text

(1) about the token paste OPERATOR ):##

To put it simply, "#" is a separate connection method. It is used to separate and then force the connection.

The separator is similar to a space. We know that in the general macro definition, the Preprocessor generally interprets space as a component segment flag. For each segment compared with the previous one, the same one will be replaced. But the result is that there are some spaces between the replaced segments. If you do not want these spaces to appear, you can add some ##to replace them.

Another separator is, including operators such as + ,-, *,/, [,],....

The macro definition has no spaces, but it still expresses a meaningful definition: Define add (A, B) A + B

The force connection function is to remove the spaces between the strings and connect them.

(2) enumeration-comparison of the following macro definitions

# Define A1 (name, type) type name _ # type ###_ type or

# Define A2 (name, type) type name ##### type ##_ type

A1 (A1, INT);/* is equivalent to: int name_int_type ;*/

A2 (A1, INT);/* is equivalent to: int a1_int_type ;*/

Explanation:

1) In the first macro definition, there is no separation between "name" and the first "_", and between 2nd "_" and the second "type, therefore, the Preprocessor interprets name _ # type ###_ type into three sections:

"Name _", "type", and "_ type". Only "type" appears before the macro.

So it can be replaced by macros.

2) In the second macro definition, "name" and the first "_" are also separated, so

The Preprocessor interprets name ##### type ##_ type into four sections: "name", "_", and "type"

And "_ type", two of which can be replaced by macros.

3) A1 and A2 can also be defined as follows:

# Define A1 (name, type) type name _ # type ### _ type

<## Add spaces at Will>

# Define A2 (name, type) type name ##### type ##_ type

The result is ## remove the leading space to complete the strong connection and obtain the same macro definition as the preceding result.

(3) other related items-a separate one #

As for a single #, this variable is replaced with double quotation marks. RatioFor example

# DEFINE _ stringify_1 (x) # x

So

_ Stringify_1 (Linux) <=> "Linux"

 

(5) # (Stringizing) string operators. The function is to convert the input parameter name in the macro definition to a parameter name string enclosed by a pair of double quotation marks. 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:

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 more than one space.

For example, STR = exapme (abc def); it will be extended to STR = "ABC Def ";

Other references

[1] http://blog.chinaunix.net/u/17855/showart_113663.html

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.