2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
The C language has some predefined symbols that can be used directly and processed during preprocessing.
__FILE__//进行编译的源文件
__LINE__//文件当前的行号
__DATE__//文件被编译的日期
__TIME__//文件被编译的时间
__STDC__//如果编译器遵循ANSI C,其值为1,否则未定义
Test Results:
#define is used to define constants:
#define MAX 100
The logical rule is to directly replace MAX with 100 in the following code. This operation step is completed in the preprocessing stage. Generally, the name of the constant defined by #define is written in uppercase.
scenes to be used:
The method of #define for defining macros is similar to that of functions, but the logic is replacement. When replacing, all variables should be enclosed in parentheses to avoid operator priority problems that may cause unexpected results.
Directions:
#define MAX(x,y) ((x) > (y) ? (x) : (y))
This is a macro that determines the larger value of two numbers. Note that the brackets after the macro name should be close to the macro.
Use Cases:
From the above two cases, we can find that macros do not restrict the type of parameters when passing them. If it is a function, the return type of the function is fixed, and the macro runs faster.
Advantages of macros:
- 1. When completing calculations with a small amount of code, macros take less time than functions.
- 2. More importantly, function parameters must be declared with a specific type. Macros can be applied to a variety of types and can be compared with >. Macros are type-independent.
Disadvantages of macros:
- Each time a macro is used, a copy of the macro definition code is inserted into the program. Unless the macro is relatively short, this may significantly increase the length of the program.
- Macros cannot be debugged.
- Since macros are type-independent, they are not very rigorous.
- Macros may cause operator precedence problems, making the program prone to errors.
Comparison table of macros and functions:
Attributes | #define defined macros | function |
---|---|---|
Code length | Each time a macro is used, it is inserted into the program. Except for very small macros, the length of the program can grow significantly. | Function code appears in only one place; each time it is used, the same code in that place is called |
Execution speed | Faster | There is additional overhead in calling and returning functions, which makes it slower |
Operator precedence | Macro parameters are evaluated in the context of all surrounding expressions. Unless parentheses are added, the precedence of adjacent operators may produce unexpected consequences, so it is recommended to use more parentheses when writing macros. | Function parameters are evaluated only when the function is called and passed to the function. Expression evaluation is predictable. |
Parameters with side effects | Parameters may be substituted in multiple locations within the macro body, and if the macro's parameters are evaluated multiple times, parameter evaluation with side effects may produce unexpected results. | Function parameters are only evaluated when they are passed, which is easy to control. |
Parameter Type | The macro parameters have nothing to do with type; as long as the operation on the parameters is legal, it can be used for any parameter type. | The parameters of a function are related to the type. If they are different, different functions are required and the tasks are different. |
debug | Macros are not easy to debug | Functions can be debugged statement by statement |
recursion | Macros cannot be recursive | Macros cannot be recursive |
The # operator converts a macro parameter to a string literal. It is only allowed in the replacement list of a macro that takes parameters.
The operation performed by the # operator can be understood as "stringification".
Example:
## combines the symbols on either side of it into one symbol, which allows macro definitions to create identifiers from separate text fragments.
Such a connection must produce a legal identifier. Otherwise, the result is undefined. Let's think about this. When writing a function to find the larger value of two numbers, different functions must be written for different data types.
Example: