# Connector and # operator in the C ++/C macro definition

Source: Internet
Author: User

Http://blog.163.com/wshyao@126/blog/static/1070451420081018103237836/

# Connector and # Connector

# The Concatenation symbol is composed of two pound signs. Its function is to connect two substrings (tokens) in the macro definition with parameters to form a new substring. But it cannot be the first or last substring. The so-called token refers to the minimum syntax unit that the compiler can recognize. The specific definition has a detailed explanation in the compilation principle, but it does not matter if you do not know. At the same time, it is worth noting that the # operator replaces the passed parameters as strings. Let's take a look at how they work. This is an example on MSDN.

Suppose the program has defined such a macro with parameters:

# Define paster (n) printf ("token" # n "= % d", token # n)

At the same time, an integer variable is defined:

Int token9 = 9;

Call this macro in the following way in the main program:

Paster (9 );

During compilation, the above sentence is extended:

Printf ("token" "9" "= % d", token9 );

Note that in this example, the "9" in paster (9); is regarded as a string and connected with the "token", thus becoming the token9. # N is also replaced by "9.

As you can imagine, the result of running the program above is to print token9 = 9 on the screen.

 

Http://kenshinf.blog.51cto.com/1088256/252541

Token paste operator: #1. 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 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
There is no space in the macro definition, but it still expresses a meaningful definition: define add (a, B) a + B and its force connection function is to remove the space between the macro and the preceding string, connect the two. 2. Compare the differences between the following macro definitions with the column name # define A1 (name, type) type name _ ## type ##_ type or
# Define A2 (name, type) type name ##### type ##_ type A1 (a1, int);/* 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 result ## the preceding space is removed to complete the forced connection, get the same macro definition as the above result 3. others-for a single # 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)
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 and define a variable using a macro. At the same time, 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 ));

 

 

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.