The first step in the birth of a program-preprocessing
The program pre-processing before compiling, this step is the preprocessing stage, mainly for some textual operations, such as the substitution of macros, inserting some of the contents of some files contained by the # include directive, as well as the processing of some conditional compilation of things. The following is a description of the main parts of the pre-processing macros and their related knowledge and some of the standard commands.
Some predefined symbols identified in the standard first:
__file__ the source file name to compile
__LINE__ the current line number of the file
Date the __date__ file was compiled
The time that the __time__ file was compiled
__stdc__ if the compilation follows ANSI C its value is 1
The use of #define
#define NAME EXP
In the preprocessing phase, the compiler replaces all the name in the file with exp (which can be either a character or an expression) as long as you can replace any text into the file with the # define directive. Both name and exp are defined by themselves.
If a line doesn't fit, you can add the character \ Next line to the last line and the compiler will think it's a thing.
For example:
#define PRINT printf ("This file is%d:" \
"X=%d,y=%d,z=%d\
__file__,__line__,\
X, Y, z)
Life like this is legal, and its print value is the print value we expect.
Macro:
In fact, the # define directive cannot be equal to a macro, and this directive contains a rule that is the use of macros,
If a macro takes a parameter then the argument list should be next to the macro name, and the parameters of the macro behind it should be enclosed with parentheses to prevent a series of problems caused by the operator's precedence.
As an example:
#define <stdio.h>
#define SUM (x) x*x
int main ()
{
int a=5;
int b;
B=sum (a+1);
printf ("%d\n", b);
return 0;
}
What exactly is printed out at this point? Some people may answer 36, in fact, bloggers in the process of learning C also answered 36 of the answer, but in fact the correct answer is 11. Please consider how this macro is replaced. In fact, a+1*a+1, the operator precedence multiplication in C is higher than the addition operation (without parentheses) This value is 11, which is far from our idea, which is why it is important to note the parentheses when declaring a macro with parameters.
#define的替换:
Be aware of the substitution process when using a macro to replace an operation:
1. When calling a macro, first check the parameters to see if any macros have been defined and if they are replaced first.
2. The replacement text is then inserted into the HM of the program, where it is completely replaced.
3. Finally, scan the text to see if it contains any symbols defined by # define, if there is a repetition of the above procedure.
In addition: macros can also be applied with strings
Macros and Functions:
Let's start by explaining several other relevant points of knowledge:
Macros in fact sometimes have side effects, do not appear in the macro X + + This statement, it will bring unpredictable adverse effects on the program.
Conventions for the naming of macros: Although HM can be customized, please name it in uppercase so that it is convenient for us to maintain the program later, we should have professional spirit.
Now, the difference between a macro and a function is the pros and cons:
Macro:
Macros are inserted into the program every time, it is possible to cause the program's code to become very large, but the macro execution speed is very fast (after all, no longer run the stage processing macros), the macro in its context should be as much as possible parentheses to prevent the operator precedence because of problems, the parameters of the macro evaluation, Macros with side effects can have undesirable effects, and macros are independent of type as long as the operation of the parameter is legal, it may be of any type. a macro cannot pass a type name and cannot be recursive.
Function:
Each invocation of the function executes the same code snippet, the return value is additional overhead, the side effect of the parameter is not affected, the value of the parameter has strict requirements for the type, and the overall run is slow (after all, it is called at run time).
#undef remove a macro that has already been declared.
#error the text of the error message to define a macro that represents an error
Conditional compilation:
#define NAME 1
#if NAME
BX7
#endif
If a macro such as name has already been defined, the compiler will automatically operate on it, and if it is true then the exp is finished and if False then the EXP is skipped.
This kind of judgment can be nested and #elif, which is actually very similar to the IF-ELSE-IF statement, in fact, the meaning is almost.
#if NAME
BX7
#elif NAME1
EXP2
#elif NAME2
EXP3
#else
EXP4
#endif
It is only after the previous judgment is false that a statement is compiled.
There is one last question for the conditional compilation:
Is whether the problem is defined, you know in the header file can not be repeated in a macro declaration, but sometimes the problem arises;
solutions;
#ifndef NAME
#define NAME
If you do not define a name, then you define a name, which effectively resolves the problem of repeatedly declaring the same macro.
Problems with the function library file and local files;
The general default #include<name.h> is the function library of the system, #include "NAME.h" is the user's local file, according to different compiler can go to set its search scope, in fact there is no functional difference even if you include the local file with angle brackets, When the compiler is not found in the library will automatically scan the current directory to find it, there are obsessive-compulsive disorder can go to Baidu to see how to change the east.
The first step in the birth of a program-preprocessing