C # define

Source: Internet
Author: User
Tags define function
# Define is a macro definition Command provided in C language. Its main purpose is to provide programmers with some convenience in programming and improve the program running efficiency to a certain extent, however, when learning this command, students often cannot understand the essence of the command, and there is always some confusion here. The command is misused during programming, so that the program runs differently from the expected purpose, or when reading programs written by others, it is difficult to understand the running results.
1 # define command profiling
1.1 # define concept
# The define command is a macro definition command in C language. It is used to define an identifier as a string. This identifier is called a macro name and the defined string is called a replacement text.
The command has two formats: A simple macro definition and a macro definition with parameters.
(1) simple macro definition:
# Define <macro name> <string>
Example: # define PI 3.1415926
(2) macro definition with Parameters
# Define <macro name> (<parameter table>) <macro>
Example: # define a (x) x
After an identifier is defined by a macro, It is a macro name. In this case, the macro name appears in the program. Before the program is compiled, replace the macro name with the defined string, which is called macro replacement, macro replacement is a simple replacement.
1.2 timing of macro replacement
To truly understand the role of # define, let's take a look at the processing process of the C language source program. When we compile the compiled source program in an integrated development environment such as Turbo C, the process of preprocessing, compilation, compilation, and connection is actually completed, as shown in figure 1.
Source program

Pre-processor

Modified source program

Compiler

Assembler

Assembler

Relocated Target Program

Connector

Executable Target Program

Figure 1 C language compilation process

The pre-processor generates the compiler output, which implements the following functions:
(1) File Inclusion
You can extend # include in the source program to the file body, that is, find and expand the contained. h file to the position where # include is located.
(2) Conditional compilation
Based on the # If and # ifdef compiling commands and their post-condition, the Preprocessor includes or excludes some part of the source program. Generally, the excluded statements are converted into empty rows.
(3) macro expansion
The pre-processor expands the macro reference in the source program file into the corresponding macro definition, that is, the # define function described in this article, which is completed by the pre-processor.
The source program processed by the pre-processor is different from the previous source program. In this stage, the work is purely replaced and expanded, and there is no computing function, so long as you can really understand the # define command, this command will not cause misunderstanding and misuse.

2 # define FAQs
2.1 problems with simple macro definition
In the use of a simple macro definition, it is easy to misunderstand and misuse when the string represented by the text is replaced by an expression. For example:
Example 1 # define N 2 + 2
Void main ()
{
Int A = N * N;
Printf ("% d", );
}
(1) A problem occurs: There is a macro definition command in this program. Macro n represents a string of 2 + 2, and there is a use of macro N in the program. when reading this program, the easy problem is that we first solve N as 2 + 2 = 4, and then use multiplication when calculating a in the program, that is, N * n = 4*4 = 16, in fact, the result of this question is 8. Why is there such a big deviation?
(2) Problem Analysis: As described in section 1, macro expansion is completed in the pre-processing phase. In this phase, the replacement text is only considered as a string, and no computation occurs, during expansion, where macro n appears, it simply uses string 2 + 2 to replace N without adding any symbols, therefore, after the program is expanded, the result is a = 2 + 2*2 + 2, after calculation = 8, which is the essence of macro replacement, how can I write a program to complete the calculation of result 16?
(3) solution: Write the macro definition in the following format:
# Define N (2 + 2)
This can be replaced with (2 + 2) * (2 + 2) = 16
2.2 problems with macro definition with Parameters
It is easy to misunderstand the use of macro definitions with parameters. For example, we need a macro to replace the square of any number, which requires parameters to replace the macro-defined parameters with actual parameters in the program. Generally, students are easy to write as follows:
# Define area (x) x * x
This is very prone to problems in use, see the following program
Void main ()
{
Int y = Area (2 + 2 );
Printf ("% d", y );
}
The parameter is 2 + 2. The result should be 4*4 = 16, but it is incorrect because the actual result of this program is 8, it still fails to follow purely simple replacement rules, and is replaced by calculation first. In this program, 2 + 2 is the parameter in the area macro, replace X in the macro definition with 2 + 2*2 + 2 = 8. If we follow the solution in (1), can we enclose 2 + 2, that is, enclose X in the macro? # Define area (x) * (x). For area (2 + 2), replace it with (2 + 2) * (2 + 2) = 16, it can be solved, but what will happen to area (2 + 2)/area (2 + 2)? Some students will give the result immediately when they see this question, because the denominator is the same, wrong again, or forget to follow the rule of first replacement and then calculation. After this question is replaced, it will become
(2 + 2) * (2 + 2)/(2 + 2) * (2 + 2) 4*4/4*4 according to the multiplication and division calculation rules, the result is 16/4*4 = 4*4 = 16. What should I do? The solution is to add a bracket to the entire macro, that is, # define area (x) * (x). Do not think this is unnecessary, without it, no.
To truly use macro definitions, remember to replace all macro usage in the program with the strings it represents when reading other programs, do not add any other symbols on your own. The running results will not be written incorrectly after the calculation is fully expanded. If the macro is replaced by a self-programmed macro, when the simple macro definition is used, when there are more than one symbol in the string, brackets are added to indicate the priority. If the macro definition with parameters is used, add brackets to each parameter in the macro and add a bracket to the entire macro. You can't help but ask if macro definition is so troublesome and error-prone. Let's look at the benefits of using macro definition in C language.
3. Advantages of macro definition
(1) convenient program modification
Use a simple macro definition to replace a constant that is frequently used in the program. In this way, when changing the constant, you do not need to modify the entire program, but only modify the macro-defined string, and when the constant is relatively long, we can use a short meaningful identifier to write the program, which is more convenient. The constant change we mentioned is not a change during the program running, but a change during the programming. For example, we are familiar with it. The circumference rate π is a commonly used value in mathematics, sometimes we use 3.14, and sometimes 3.1415926. This depends on the accuracy required for calculation. If a program we compile needs to be used multiple times, so you need to determine a value that will not be changed in this operation, but you may find that the accuracy of the program has changed and you need to change its value,
This requires modifying all the values in the program, which may cause some inconvenience. However, if you use a macro definition instead of an identifier, you only need to modify the macro definition, we can also reduce the number of input values such as 3.1415926 for multiple times. We can define # define PI 3.1415926 as this reduces the number of inputs and facilitates modification. Why not?
(2) Improve program running efficiency
Using macro definitions with parameters can call functions, reduce system sales, and improve operation efficiency. As mentioned in C language, the use of functions can make the program more modular, easy to organize, and reusable. However, in the case of function calls, the field of function calls must be retained, so that after the sub-function is executed, it can return and continue execution. It also takes some time to restore the called function after the sub-function is executed, if the sub-function executes a large number of operations, the overhead of the conversion time can be ignored. However, if the sub-function does not have enough functions, you can perform only one operation, such as a multiplication statement operation, this part of the conversion overhead is relatively large, but this problem will not occur when the macro definition with parameters is used.
Because it is expanded in the pre-processing stage, and does not need to be converted during execution, that is, it is executed locally. Macro definition can complete simple operations, but complex operations must be completed by function calls, and the macro definition occupies a large space for the target code. Therefore, you must determine whether to use macro definitions based on specific conditions.
4 Conclusion
In this paper, the macro definition # define in C language is parsed to solve problems that may occur during use, and the # define processing is analyzed from the perspective of the C source program processing process, it also describes its advantages. As long as you can understand the macro expansion rules and master the use of macro definition, the source program is replaced in the pre-processing phase, but the macro name in the program is replaced with the corresponding string, in this way, you can fully enjoy the convenience and efficiency of using macro definitions on the basis of correct use.
II.
I recently read the com-related documents. When I saw that csf-target implemented the COM interface, I read some macro definitions in the afxdisp. h header file.

# Define begin_interface_part (localclass, baseclass )/
Class X # localclass: Public baseclass/

This macro definition is easy to understand, but here is an extra X #. I have never seen such a definition, and I don't know what it means.
I asked a few friends, but I didn't know either.

Do you know?

Maybe you don't know ~ Well, I finally found the relevant materials, explained the define, and also met the other two less commonly used define

# Define conn (x, y) x # Y
# Define tochar (x) # @ x
# Define tostring (x) # x

X ## what does y mean? X connects to Y, for example:
Int n = conn (123,456); the result is n = 123456;
Char * STR = conn ("ASDF", "ADF") returns STR = "asdfadf ";
How about it? It's amazing.

Let's take a look at # @ X. In fact, a single quotation mark is added to X, and the returned result is a const char. For example:
Char A = tochar (1); the result is a = '1 ';
Make an out-of-bounds test char a = tochar (123); the result is a = '3 ';
However, if your parameter contains more than four characters, the compiler reports an error! Error c2015: Too character characters in constant: P

Finally, let's take a look at # X. You may also understand that it adds double quotation marks to X.
Char * STR = tostring (123132); then STR = "123132 ";

Finally, let's leave a few small tests for everyone to test:
# Define Dec (x, y) (x-y)
Int n = Dec (A (1230 );
N = conn (123, Conn (123,332 ));
Char * STR = a ("12", tostring (Dec (3, 1 ));
What will happen? Hey hey ~

III.
# Define XXX (){}
Supported by standard C
# Define XXX ()({})
The new features of GCC are mainly used to prevent macro expansion problems. By default, one feature is added during expansion, which is prone to problems.

Code: # define a (A, B, C) ({A = 1; B + = 1; C = 3; A + B + C ;})
# Include <stdio. h>
Int main ()
{
Int;
Int B = 1;
Int C;
Int D;
D = a (A, B, C );
Printf ("% d, % d/N", A, B, C, D );
Return 0;
}
It indicates that the macro function still has a return value. the return value of the last sub-statement is used as the return value of the macro function.
Running result:
1, 2, 3, 6


For your security, please only open the URL with reliable source

Cancel website opening

From:
Http://hi.baidu.com/%CB% AE %B2%BB%D7%AA%C9%BD%D7%AA/blog/item/36bc04ad87688df7faed5050.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.