# In macro definition #

Source: Internet
Author: User
#, # @ And \ symbols in the macro definition of C LanguageSource: Liu shihui Rubby logs

1. # (stringizing) string Operators

 

Purpose: 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 of spaces:

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 a space to connect each sub-string to ignore the remaining space.

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

2. ## (token-pasting) Symbolic join Operator

Purpose: 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:

A. When ## is used to connect a form parameter, spaces before and after ## are optional.

For example: # define examplenum (n) num # N is equivalent to # define examplenum (n) num # N

B. The actual parameter name after connection must be an existing parameter name or a macro defined by the compiler.

In addition, if the parameter after # Is a macro, # will prevent the macro from being expanded.

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

Int main ()
{
Char var1_p [20];
Char var2_p [30];
Strcpy (var1_p, "aaaa ");
Strcpy (var2_p, "BBBB ");
Strcpy (var1, var2 );
Strcpy (var2, var1 );
Printf ("var1 = % s \ n", var1_p );
Printf ("var2 = % s \ n", var2_p );
Return 0;

Strcpy (var1, var2), var2 );
// Will the strcpy (var1_p, "var2") _ p, "var2") be displayed here? The answer is no:
// The expanded result will be: strcpy (var1, var2) _ p, "var2 ")
// ## The macro expansion of parameters is blocked! If # And # are not used in the macro definition, the macro will be fully expanded.
}

3. # @ (charizing) character Operators

Purpose: Convert the Input Single-character parameter name to a character and enclose it with a single reference.

For example:

# Define makechar (x) # @ x

A = makechar (B );

Expanded:

A = 'B ';

4 ,\Continuation Operator

When the defined macro cannot be fully expressed in one row, you can use "" to indicate that the next row will continue the definition of this macro. Note: Leave a space before.

Reference: http://blog.csdn.net/xdsoft365/archive/2010/09/28/5911596.aspx

Http://blog.csdn.net/leng_que/archive/2010/11/06/5991697.aspx

Http://yuxu9710108.blog.163.com/blog/static/237515342010629103421876/

Http://blog.csdn.net/billpig/archive/2010/11/27/6039611.aspx

 

Source Address: http://blog.renren.com/GetEntry.do? Id = 736064665 & owner = 382496004

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.