Macro definition
A macro definition is a definition of some common variables, strings, and so on, and the defined data is automatically replaced in the compilation. Sometimes some variables or strings are used more than once, when the need to modify, you need to the source files where they appear in one by one, the efficiency is low, and through the macro definition, only need to modify the definition, you can achieve batch modification, improve efficiency, or sometimes some numbers or characters need multiple input, When numbers or characters are not very regular, the input is cumbersome and error-prone. The Hongyi will facilitate input and maintenance.
The format of the definition
#define Identifier string
Where the "#" indicates that this is a preprocessing command. All that begin with "#" are pre-processing commands. "Define" defines the command for the macro. "Identifier" is the name of the macro defined. A "string" can be a constant, an expression, a format string, and so on.
Precautions
1. The macro definition is simply a substitution, and the preprocessor does not make any checks on it. You can only find errors when compiling a source program that has been expanded by the macro.
2. The macro definition is not a description or statement, there is no need to add a semicolon at the end, such as a semicolon is also replaced with a semicolon.
3. The macro defines its scope as a macro-defined command that ends with the source program. You can use the #undef command if you want to terminate its scope.
Working with instances
Macros are defined in order to handle expressions with precedence, and should be bracketed to avoid the occurrence of a priority error when the macro is expanded. For example, the following code.
#define M a+bs=m*m; // equivalent to S=a+b*a+b ////////////////////////////////////////// / #define M (a+b)s=m*m; // equivalent to s= (a+b) * (a+b) ////////////////////////////////////////// /
The C language allows macros with parameters. The parameters in the macro definition are called formal parameters, and the parameters in the macro call are called actual parameters. For a macro with parameters, in the call, not only the macro expansion, but also with the actual parameter to replace the parameter. See the code below.
#define C (x) (3.14*x*2)C=c (5)// equivalent to c= (3.14*5*2)
///////////////////////////////////////////
#define S (x, Y) ((×) * (y))
s=s (2+3,4+4)
// equivalent to s= ((2+3) * (4+4))
///////////////////////////////////////////
It is not possible to define macros such as variables, operands, or expressions that can change when called, as in the following code.
#define MIN (A) < (B) ( A):(B))int a=1, b=5, min=0; min=min (a++, B);
// after macro expansion, the expression is: max= ((a++) < (b)? ( a++):(B))
The return value is min=3
////////////////////////////////////////// /
Conditional compilation
Conditional compilation can also be done in a header file by using a combination of #ifdef, #define和 #endif. The purpose of conditional compilation is to prevent multiple files from repeating a reference to this same header file. The common format is as follows.
#ifndef <</SPAN> identity >#define <</SPAN> identity >... // include or define STH. #endif
preprocessing command [#define] Description