There are three kinds of preprocessing functions in C: 1. File contains # include 2. Macro Definition # # 3. Conditional Compilation # if #ifdef等.
preprocessing is pre-processing by the preprocessor prior to being compiled by the compiler. The Preprocessing code contains "#" as the symbol flag to differentiate the other source code.
The benefits of preprocessing are mainly to facilitate the maintenance of the readability of the program, to facilitate developers to develop code, but also to improve the efficiency of the compilation.
The process of preprocessing is primarily to instruct the preprocessor how to modify the source code. Before the program is normally compiled, the compiler will run the preprocessor automatically and pre-compile the program.
For example, when using the # include file, the include file is replaced with the corresponding location of # include before compiling, and when you use # define, the macro is replaced with the actual parameters before compiling: When using conditional compilation, it is determined whether the compiler compiles the current code snippet.
1. #include header file
For the benefits, sources, and usage of the header file, refer to the http://blog.csdn.net/pashanhuxp/article/details/39927913 article
Add:
①. #include <XXX.h> and #include "XXX.h" differences:
<XX> represents the lookup. h header file in the compiler-defined reference directory;
"XX" means to find the. h header file in the current directory of the project, if it does not go to the compiler specified directory lookup;
Therefore, if you refer to a custom. h header file, you can only use the # include "XX".
②. Under Eclipse CDT, #include "XX.h" Guidelines for header files in the Usr/include directory, do not include the header files of subfolders, and if you refer to a header file under a subfolder, you need to change to include "Xx/xx.h"
2. #define MACRO Definition:
A macro definition can also be called a "macro substitution", and the macro is replaced by a preprocessor before it is compiled. Usually in uppercase notation
Macro definitions can increase the readability and maintainability of the program, such as #define PI 3.14159
and the macro substitution function can improve the efficiency of the operation and reduce the overhead of the function call. For example #define SUM (a,b,c) (A+B+C) is more efficient than defining a sum function int sum (A,B,C) to save memory. Here you need to pay attention to the meaning of substitution, the macro is directly replaced, such as #define AA A-B without parentheses, in the course of use, there will be cc*aa into cc*a-b error.
Add:
① empty macro defines the modifier function:
The code sometimes appears #define SUM does not give the macro sum "assignment". At this point, sum can be understood as empty, meaningless.
For example, to modify a function: Sum int getsum (A, A, b) can then interpret the function of sum as a simple description of what functions do, making the caller more aware
② empty macro definitions are combined with conditional compilation:
#ifndef _8_4_2_h_#define _8_4_2_h_#include <stdio.h> #include <string.h> #endif/* 8_4_2_h_ * *
in this case, the _8_4_2_H_ macro is an empty macro for conditional compilation, and it is also common to prevent the use of header files for repeated inclusion.
Give you an example, and then, by the way, analyze:
Suppose you have 4 files in your project, namely A.cpp,b.h,c.h,d.h.
The head of the A.cpp is:
#include "B.h"
#include "c.h"
The heads of B.h and c.h are:
#include "D.h"
And D.h has the definition of Class D.
So
Compiler compile A.cpp, the first according to # # # "B.h" to compile b.h this problem, and then according to B.h inside the # include "D.h", to compile d.h this file, so that the d.h inside the class D compiled;
Then, according to the second sentence of A.cpp, "c.h", to compile c.h, eventually will find the d.h inside the Class D, but the Class D has been compiled before, so will report a redefinition error.
If you add ifndef/define/endif to the d.h, you can prevent this redefinition error.
③ also has some compiler-defined macros in the format "start with a double underscore". Primarily identifies some compilation environment information. Less to use.
3.# Conditional Compilation
When you use conditional compilation, you determine whether the compiler compiles the current code snippet. Improve compilation efficiency.
#ifdef conditional Code Snippet ... #endif
Explanation: If the macro defines a condition, the code snippet is executed, otherwise it is not executed.
#if conditional Code Snippet ... #endif
Explanation: If the condition is true, the code snippet is executed, otherwise it is not executed.
Examples of Use:
#if 0 a#endif</span>
in this code, a never executes, this is written to facilitate debugging changes later, if you want to execute a, you can only change to #if 1.
Preprocessing code for C language