Once launched, C ++ has become a leader in the development field by virtue of its own performance advantages. In this article, we will introduce the functions of the C ++ preprocessing commands in macro operations. I hope this will help you understand this knowledge easily.
- C ++ static member initialization FAQs
- Basic concepts of C ++ type conversion
- C ++ compiler command list Summary
- C ++ basic concepts of time
- Detailed explanation of C ++ timing operations
In C ++, the macro summarizes various compilation commands in the source code of the C program. These commands are called preprocessing commands. Although they are not actually part of the C language, they extend the C programming environment. This section describes how to use preprocessing programs and annotations to simplify the program development process and improve the readability of the program.
Macros are not part of the C ++ language and are pre-processed by the processor before compilation. Some functions of macros can be replaced by template, inline, and const in C ++. In my opinion, the greatest use of C ++ is to automatically generate ANSI-defined C ++ preprocessing commands, including:
# Define, # error, # I nclude, # if, # else, # elif, # endif, # ifdef, # ifndef, # undef, # line, # pragma, etc.
Obviously, all the pre-processing commands start with #. Next we will introduce the first three commonly used processing commands.
C ++ pre-processing command 1, # define
Command # define defines an identifier and a string. Each time This identifier is encountered in the source program, it is replaced by a defined string. The ANSI standard defines the identifier as a macro name, and the replacement process is called macro replacement. The command is generally in the following format:
# Define identifier string
Note:
This statement does not have a semicolon. There can be any space between the identifier and the string. Once the string starts, it ends only with a new row.
After the macro name is defined, it can become part of other macro name definitions.
Macro replacement only replaces macro identifiers with text strings, provided that the macro identifiers must be identified independently. Otherwise, the macro identifiers are not replaced. For example:
# Define XYZ this is a test, use the macro printf ("XYZ"); // this section does not print "this is a test" and prints "XYZ ". Because the pre-compiler identifies "XYZ"
If the string is longer than one row, you can use a backslash '\' to continue the row at the end of the row.
C ++ pre-processing command 2, # error
The processor command # error forces the Compilation Program to stop compilation, which is mainly used for program debugging.
C ++ preprocessing command 3, # I nclude
The command # I nclude enables the compiler to embed another source file into a source file with # I nclude. The source file to be read must be enclosed by double quotation marks or angle brackets. For example:
# Include "stdio. h" or # include
Both lines of code use the C compiler to read and compile subprograms used to process Disk File libraries.
Embedding a file in the # include command is feasible. This method is called a nested embedded file. The nested hierarchy depends on the specific implementation.
If the explicit path name is part of the object identifier, search for the embedded object in only the subdirectories. Otherwise, if the file name is enclosed in double quotation marks, the current working directory is retrieved first. If no file is found, search all directories described in the command line. If no file is found, search for the standard directory defined during implementation.
If there is no explicit path name and the file name is enclosed by Angle brackets, it is first retrieved in the directory of the compiled command line.
If the file is not found, the standard directory is retrieved and the current working directory is not retrieved.
The above is a brief introduction to the three commonly used C ++ preprocessing commands.