Solo wind
Links: C Language Macro definitions (1)
1, why should there be a macro definition?
A particular value in the code needs to participate in the operation, and the value in many places, when it is necessary to modify the value, you want to change only one place to achieve the full update of the value, even if a value is used only once, when the changes will be faced with the search to read a large number of code, the numerical meaning is not clear problems; "When encapsulated into a function, calling function overhead (save context, parameter calls, stack allocations, etc.) is too large to affect efficiency, does not encapsulate the need to enter the same" action block "content more than once, and to modify the trouble prone to inconsistencies, it is necessary to" Operation block "definition macro, the scheme is essentially" space for Time ".
In short, the appearance of macros in order to: Provide code readability, increase the efficiency of code writing and improve the efficiency of system operation.
2. What is the background of macro production?
The essence of the macro is "unconditional substitution", and the macro only works on the text of the program, its eyes of the world only text (characters), there is no logical operation. Remember to learn C, the teacher told us: "Every sentence after the semicolon"; ", but the macro definition can not add," Asked why it is: "is not, remember on the line! “。 Now you know why: If you add a semicolon, the semicolon becomes part of the macro definition, and when you do a "macro unwind", the statement is truncated, of course there is a problem.
To be exact, the program source code (*.C file) cannot be executed directly by the computer, and the compiler needs to do a "series of processing" of the program code to convert it into a binary file (*.bin) that can be read by the computer. This involves the knowledge of the compiler principle (I will explain the compilation principle if there is a chance), a series of processing includes: preprocessing (preprocessing), compiling (compilation), assembly (Assembly), link (linking). The "macro expansion" process occurs in the "preprocessing" phase, the compiler is responsible for replacing the macro name with the actual data, so after the "preprocessing", there is no so-called macro concept in the function code.
I'll do an experiment to verify that:
(1) In Fedora Environment, type "VI test_macro.c" to enter the following code and save
(2) Exit VI environment, type "GCC-E test_macro.c" execution, the effect is as follows
(3) test results analysis:
The "gcc-e test_macro.c" command tells the compiler to preprocess only the code, and no further direct output is performed. As you can see, our defined macro pi and r are no longer present, and are replaced with their respective constant values. The function declaration above the main function is the expansion of the stdio.h file, the preprocessing phase is not only macro expansion, but also the included header files are expanded, here no longer elaborate.
In the next section, we will combine the test data to analyze some of the considerations in the macro usage process.
Reprint--C Language macro definition (1)