Macro-defined operators ###

Source: Internet
Author: User

A macro definition can contain two operators: # And ##.

 

# The operator converts a macro parameter to a string literal. It can only appear in the replacement list of macros with parameters. # Operators have a lot of usage. Here we will only discuss one of them.

Suppose we decided to use the PRINT_INT macro as a convenient method during the debugging process to output the value of an integer variable or expression. # The operator allows PRINT_INT to add labels for each output value. The improved PRINT_INT is as follows:

# Define PRINT_INT (x) printf (# x "= % d \ n", x)

The # operator before x notifies the Preprocessor to create a string literal Based on the PRINT_INT parameter. Therefore

PRINT_INT (I/j );

Will change

Printf ("I/j" "= % d \ n", I/j );

In C, the adjacent strings are merged, so the preceding statement is equivalent:

Printf ("I/j = % d \ n", I/j );

 

# The operator can "stick" two marks, for example, identifiers, to form a mark. If one of the operands is a macro parameter, "bonding" will occur after the formal parameter is replaced by the actual parameter. Consider the following macros:

# Define MK_ID (n) I # n

When MK_ID is called, for example, MK_ID (1), the Preprocessor first replaces the parameter n with the independent variable (1. Then, the pre-processor connects I and 1 into a mark i1 ). The following statement creates three identifiers using MK_ID:

Int MK_ID (1), MK_ID (2), MK_ID (3 );

After preprocessing, the statement becomes:

Int i1, i2, i3;

# Operators are not frequently used by preprocessors. In fact, it is difficult to find some situations to use it. In order to find a practical # application, let's rethink the previously mentioned MAX macro. As we can see, when the MAX parameter has side effects, it will not work normally. One solution is to use the MAX macro to write a max function. Unfortunately, a max function is often not enough. We may need a max function whose actual parameter is the int value, a max function whose parameter is the float value, and so on. Except for the actual parameter type and return value type, these functions are the same. Therefore, it seems silly to define every function.

The solution is to define a macro and expand it to become the definition of the max function. A macro has a unique parameter type, which indicates the type of the formal parameter and return value. There is another problem here. If we use a macro to create multiple max functions, the program cannot be compiled. C language does not allow two functions with the same name to appear in the same file .) To solve this problem, we use the # Operator to construct different names for the max functions of each version. The following is the macro display format:

# Define GENERIC_MAX (type )\

Type ##_ max (type x, type y )\

{\

Return x> y? X: y ;\

}

Note how to connect type and _ max in macro definition to form a new function name.

Now, if we need a max function for the float value. The following describes how to use the GENERIC_MAX macro to define a function:

GENERIC_MAX (float)

The pre-processor expands this line into the following code:

Float float_max (float x, float y) {return x> y? X: y ;}


This article is from the "Sun's technology blog" blog, please be sure to keep this source http://sunke.blog.51cto.com/4812218/1290137

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.