Technology Sharing

C language preprocessing

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Predefined symbols

The C language has some predefined symbols that can be used directly and processed during preprocessing.

__FILE__//进行编译的源文件
__LINE__//文件当前的行号
__DATE__//文件被编译的日期
__TIME__//文件被编译的时间
__STDC__//如果编译器遵循ANSI C,其值为1,否则未定义
  • 1
  • 2
  • 3
  • 4
  • 5

Test Results:
insert image description here

2. #define constants

#define is used to define constants:

#define MAX 100
  • 1

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:
insert image description here

3. #define macro definition

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))
  • 1

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:
insert image description here
insert image description here

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.

4. Comparison of Macros and Functions

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:

    1. 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.
    1. Macros cannot be debugged.
    1. Since macros are type-independent, they are not very rigorous.
    1. Macros may cause operator precedence problems, making the program prone to errors.

Comparison table of macros and functions:

Attributes#define defined macrosfunction
Code lengthEach 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 speedFasterThere is additional overhead in calling and returning functions, which makes it slower
Operator precedenceMacro 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 effectsParameters 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 TypeThe 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.
debugMacros are not easy to debugFunctions can be debugged statement by statement
recursionMacros cannot be recursiveMacros cannot be recursive

5. # and ##

1. # operator

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:
insert image description here

2. ## operator

## 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:
insert image description here