C ++ provides the following three types of compilation preprocessing functions:
(1) macro definition
(2) File Inclusion
(3) Conditional compilation
In C ++, we generally use const to define symbolic constants. Obviously, defining constants with const is better than defining constants with define.
Note the following when using macro definition:
(A) When writing the # define command, note that <macro name> and <string> are separated by spaces instead of equal signs.
(B) The identifier defined by # define is not a variable. It is used only for macro replacement and therefore does not occupy the memory.
(C) habits use uppercase letters to indicate <macro name>. This is just a habit of Convention. Its purpose is to distinguish it from the variable name, because the variable name
Generally, lowercase letters are used.
If an identifier is defined as a macro name, you cannot define it again before canceling the macro definition. To cancel macro definition, run the following command:
# UNDEF <identifier>
Among them, UNDEF is the keyword. The function of this command is to cancel the <identifier> existing macro definition. The identifier of the macro definition is canceled and can be redefined.
Macro definitions can be nested. defined identifiers can be used to define new identifiers. For example:
# Define PI 3.14159265
# Define R 10
# Define area (pI * r)
A single macro definition defines an identifier as a string. The identifier in the source program is replaced by a specified string. As mentioned earlier, the pre-processing commands are different from general C ++ statements. Therefore, the pre-processing command usually has no extra points. This does not mean that all preprocessing commands cannot contain semicolons. The macro definition simply replaces a string with the macro name. Therefore, if a semicolon is added after the macro definition command, it will be replaced with the semicolon.
Macro definition with Parameters
The macro definition with parameters is generally in the following format:
# Define <macro name> (<parameter table>) <macro>
The <macro name> is an identifier, and the <parameter table> can contain one or more parameters, depending on the specific situation. When there are multiple parameters, each parameter is separated by a comma. <Macro> is a replacement string. the string in the macro is an expression composed of parameters in the parameter table. For example:
# Define sub (a, B) a-B
If the following statement appears in the program:
Result = sub (2, 3)
Is replaced:
Result = 2-3;
If the following statement appears in the program:
Result = sub (x + 1, Y + 2 );
Is replaced:
Result = x + 1-y + 2;
In this macro replacement process, the parameters in the parameter table are simply substituted into the macro expression. In the above example, A and B in the expression are substituted into 2 and 3 respectively.
We can find that macro definitions with parameters are similar to functions. If we regard the parameters that appear during macro definition as form parameters, the parameters that appear when macro definition is referenced in a program are considered as real parameters. In the preceding example, A and B are form parameters, while 2 and 3, and x + 1 and Y + 2 are both real parameters. When the macro is replaced, the form parameter in <macro> is replaced with the real parameter.
When using macro definitions with parameters, note the following:
(1) The macro with parameters should be written in one row. If you need to write in multiple rows, use the "\" end when each row ends.
And press the Enter key after the symbol, except for the last line.
(2) When writing a macro definition with parameters, no space is allowed between the <macro name> and the left parenthesis. Otherwise, the right part of the space is used as the macro.
For example:
# Define add (x, y) x + y
The whole of "(x, y) x + y" is used as the defined string.
(3) when defining a macro with parameters, it is very important to properly add parentheses to the string with the same name as the macro, so as to avoid
Possible errors. For example, for macro definition:
# Define Sq (x) x * x
When the following statements appear in the program:
M = SQ (A + B );
The replacement result is:
M = a + B * A + B;
This may not be the expected result. If you need the following replacement result:
M = (a + B) * (A + B );
Macro definition should be modified:
# Define Sq (x) * (X)
To replace macro definitions with parameters, if a macro with real parameters (such as "sub (2, 3)") exists in the program )"), then, replace the string specified in the "# define" command line from left to right. If the string contains the form parameters (such as a and B) in the macro, the real parameters (such as constants, variables, or expressions) in the Program Statement are replaced by the form parameters, if the character in the macro definition is not a parameter character (such as the-number in a-B), it is retained. This forms a replacement string.