First, the compilation preprocessing:
1. Macro definition : Start with #define, divided into two types with parameters and without parameters.
Macro definition without parameters: #define PI 3.14
Macro substitution is simply a simple substitution between a macro reputation string, no other data type and legality checks, and no memory space allocated.
Macro definition with Parameters: #define MUL (x, y) (x) * (y)
Note: When writing with a parameter macro definition, there is no space between the macro name and the opening parenthesis, or the character to the right of the space Nonalphanumeric back as part of the substitution string.
#define Add (x, y) X+y//will consider replacing the add macro with (x, y) x+y
The difference between a macro and a function with parameters:
- function calls are processed at the time the program is run, and the internal deposit cells are allocated in the stack. A macro band loop is done before compilation, and does not allocate memory units at the time of substitution, does not pass value, and does not return a worthwhile concept.
- When a function is called, the value of the argument expression is calculated first and then assigned to the parameter. The macro is simply a replacement and does not do any calculations.
- Both the formal and the actual parameters in the function must have a type, and the two should be consistent, and if not, the system will automatically convert the type. There is no type problem with macros.
- Macro substitution only takes up compile time because it is done before compiling, and function calls take up the elapsed time.
#define #undef (undef the scope of the macro definition prematurely).
2. File contains : #include < file name > or #include "file name"
3. Conditional compilation : Only program segments that meet the conditions in the original program are compiled. 1. Reduce the program run time memory by making the generated target program shorter. 2. Easy to Debug.
- Determines whether certain segments are compiled depending on whether the macro name is already defined
- Jump over for a moment.
C + + Basic---Part 4