The C preprocessor is not part of the compiler and is a separate step in the compilation process. The C preprocessor is just a text replacement tool that instructs the compiler to complete the required preprocessing before actual compilation.
All preprocessor commands begin with a pound sign (#). It must be the first non-null character, and in order to enhance readability, preprocessor directives should start with the first column.
The following table contains all the important preprocessor directives:
Instructions |
Describe |
#define |
Defining macros |
#include |
Contains a source code file |
#undef |
Cancel a defined macro |
#ifdef |
If the macro is already defined, return the true |
#ifndef |
If the macro is not defined, return the true |
#if |
If the given condition is true, compile the following code |
#else |
Alternatives to #if |
#elif |
If the previous #if the given condition is not true and the current condition is true, compile the following code |
#endif |
End a #if ... #else conditional compilation block |
#error |
Output error message when standard error is encountered |
#pragma |
Use the standardized method to publish special commands to the compiler |
#line |
Provides the line number for compiler information |
Predefined macros
ANSI c defines a number of macros. You can use these macros in programming, but you cannot directly modify these predefined macros.
Macro |
Describe |
__date__ |
The current date, the amount of the word constants in the "MMM DD YYYY" format. |
__time__ |
The amount of Word constants in the current time, in "HH:MM:SS" format. |
__file__ |
Contains the current file name, string constant. |
__line__ |
Contains the current line number, the decimal constant. |
__stdc__ |
When the compiler compiles in ANSI standard, it is defined as 1. |
__stdc__version__ |
If the compiler follows C99, the value of the macro is 199901L, and in other cases, the macro is undefined. |
__stdc__hosted__ |
The current host system, which has a value of 1, is currently a stand-alone system with a macro value of 0. |
__stdc__iec__559__ |
If the floating-point implementation follows the IEC 60599 standard, the macro value is 1, otherwise there is no definition. |
__stdc__iec__559__complex__ |
If the complex operation is implemented in accordance with the IEC 60559 standard, the macro value is 1, otherwise undefined. |
__stdc__iso10646__ |
is defined as a long integer constant. |
Preprocessor operators
The C preprocessor provides the following operators to help create macros:
1. Macro continuation operator (\)
A macro is usually written on a single line. However, if the macro is too long for a single row to fit, the macro continuation operator (\) is used.
2, string constant quantization operator (#)
In a macro definition, when you need to convert a macro parameter to a string constant, the string literal operator (#) is used. The operator used in a macro has a specific parameter or parameter list.
3, Tag paste operator (# #)
The tag paste operator (# #) within a macro definition merges two parameters. It allows two separate tags in a macro definition to be merged into one tag.
4, defined () operator
The preprocessor defined operator is used in a constant expression to determine whether an identifier is already in use #define定义过. If the specified identifier is defined, the value is true (not 0). If the specified identifier is undefined, the value is False (0).
Parameterized macros
CPP A powerful feature is that you can use parameterized macros to simulate functions.
Before you use a macro with parameters, you must use the #define Directive definition. The argument list is enclosed in parentheses and must be immediately after the macro name. Spaces are not allowed between the macro name and the left parenthesis.
The above in-depth understanding of the C preprocessor is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.