Macro-defined string operations in C/C ++

Source: Internet
Author: User

Token paste OPERATOR ):##

1. To put it simply, "#" is a separate connection method. Its function is to first separate and then force the connection.

The separator is similar to a space. We know that in the general macro definition, the Preprocessor generally uses spaces
The interpretation component segment flag. If each segment is compared with the previous one, it will be replaced. But the result is,
There are some spaces between the replaced segments. If we do not want these spaces to appear, we can add some
# To replace spaces.

Other separator signs include operators such as +,-, *,/, [,],…, So even though the following
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. Column-test the differences between 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 a gap between "name" and the first "_", and 2nd "_" and the second one.
"Type" is not separated, so the Preprocessor will interpret name _ # type ###_ type as three segments:
"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-a separate one #

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

# DEFINE _ stringify_1 (x) # x
So
_ Stringify_1 (Linux) <=> "Linux"

Therefore, for module_device_table

1) # define module_device_table (type, name)
Module_generic_table (type ##_ device, name)
2) # define module_generic_table (gtype, name)
Extern const struct gtype ##_ ID _ mod _ ## gtype ##_ table
_ Attribute _ (unused, alias (_ stringify (name ))))

Get
Module_device_table (USB, products)
/* Notes: struct usb_device_id products ;*/
<==> Module_generic_table (usb_device, products)
<==> Extern const struct usb_device_id _ mod_usb_device_table
_ Attribute _ (unused, alias ("products ")))

Note that alias attribute requires a double quotation mark, so here _ stringify (name) is used
Add double quotation marks to name. In addition, we also noticed that an external variable "_ mod_usb_device_table" is alias
The user-defined variable products <usb_device_id type> exclusive to the driver. This external variable
For more information, see probe () Process Analysis.

4. Analysis Method and verification method-compile a simple C program

Define a variable using a macro, define the same variable in a direct way, and compile the report for repeated definitions;
Use a macro to define a variable, directly use the variable name defined by the macro, compile and run the result correctly;
Print string data using printf. Printf ("token macro is % s", _ stringify_1 (A1 ));

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/wesleyluo/archive/2010/05/21/5613842.aspx

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.