preprocessing is the process before compiling, and one of the tasks of compiling is grammar checking and preprocessing without grammar checking . The preprocessing command begins with the symbol "#" .
Common pre-processing directives include:
- Macro definition: #define
- File contains: #include
- Conditional compilation: #if, #elif, #ifndef, #ifdef, #endif, #undef
- Error message directive: #error
- #line指令
- Layout control: #pragma
Macro definition
A macro definition is also called a macro substitution , a macro substitution , or a macro. Macro substitution is only used for substitution, no calculation, no expression solving . A macro defines a macro definition with a split parameter and a macro definition without parameters. In a macro definition with parameters, there must be no spaces between the macro name and the parentheses of the parameter.
The macro definition does not allocate memory , and variable definitions allocate memory.
macro expansion does not take up run time, accounting for compile time, function calls take up run time (allocate memory, hold field, Value Pass, return value).
The #运算符that appears in the macro definition converts the following arguments into a string, and sometimes the # of this usage is called the string operator . For example:
#include <iostream>usingnamespace std; #define STR (n) "ABCD" #nint main () { cout<<str (6) <<Endl; System ("pause"); return 0 ;}
The output is:
# #运算符used to connect the parameters together, the preprocessor appears in the # #两侧的参数合并成一个符号. For example:
#include <iostream>usingnamespace std; #define STR (a,b,c) a# #b # #cint main () { cout<<str (1,2,3) << Endl; System ("pause"); return 0 ;}
The output is:
file contains
#include < file name > is called the standard way, to the system header file directory to find files, #include "file name" first in the current directory (user path) to find, and then to the system header file directory lookup.
We take #include<iostream.h> and #include<iostream> as an example to illustrate: Iostream.h is a C-language header (library) file, for the old version of the standard library, only support narrow character sets Whereas iostream is a standard header file for C + +, it supports narrow character sets and wide character sets.
static global variables in the included file are not declared in the include file .
Conditional compilation
Using conditional compilation can make the target program smaller and the running time shorter.
#undef指令, which is used to delete a predefined macro definition , in its general form: #undef Macro Replacement name
Error message directive
#error指令 , #error指令就停止编译. Its general form is : #error error message.
#ifndef __cplusplus #error This was not a C + + complier. #endif #include<iostream>usingnamespace std; int Main () { system ("pause"); return 0 ;}
#line指令
The command #line change the contents of _line_ and _file_ , which are pre-defined identifiers in the compilation program.
The format is: #line number [filename], which can change the current line numbers and file names.
#include <iostream>usingnamespace std; #line "a.cpp"int main () { cout<<__LINE__<<'\ t '<<__FILE__<<Endl; System ("pause"); return 0 ;}
Operation Result:
Layout control directives
In all pre-processing directives, the #Pragma directive may be the most complex, and its role is to set the state of the compiler or to instruct the compiler to complete certain actions . #pragma指令对每个编译器给出了一个方法, given the unique characteristics of the host or operating system, while maintaining full compatibility with the C and C + + languages. By definition, the compilation instructions are proprietary to the machine or operating system and are different for each compiler.
The format is generally: #pragma Para, where Para is the parameter.
message parameter : output the appropriate information in the Compile Information Output window
#pragma message ("text")
code_seg parameter : set the code snippet where the function code is stored in the program and use it when we develop the driver
#pragma code_seg (["Section-name" [, "Section-class"]]
#pragma once:
Simply adding this instruction at the very beginning of the header file will ensure that the header file is compiled once, but the portability is poor. If you are writing programs that cross-platform, it is best to use macro definitions in C + +.
#pragma hdrstop: Indicates that the precompiled header file ends, and the subsequent header files are not precompiled.
#pragma resource:
#pragma resource "WINFORM.DFM"
Indicates that the resources in the WINFORM.DFM file are added to the project, including the definition of the form's appearance in WINFORM.DFM.
#pragma warning: output warning message.
Let's Run the program:
int Main () { float f=3.6; int i=f; cout<<i<<Endl; System ("pause"); return 0 ;}
The following warning message appears:
1>clcompile:1>f.cpp1>c:\users\gaohongchen\desktop\45 \5\5\f.cpp (N): Warning C4305: "Initialize": Truncate from "double" to "float" 1>c:\users\gaohongchen\desktop\\5\5\f.cpp ( ): Warning C4244: "Initialize": Convert from "float" to "int", possibly losing data
If we add it above the program:
#pragma Warning (disable:4305)
The compile-time warning message changes to:
1>clcompile:1>f.cpp1>c:\users\gaohongchen\desktop\45 \5\5\f.cpp:warning C4244: "Initialize": Convert from "float" to "int", Possible data loss
If we add it above the program:
#pragma Warning (error:4244)
The warning message becomes an error message:
1 >clcompile: 1 >f.cpp 1 >c:\users\ Gaohongchen\desktop\45 \5 \f.cpp ( "to" float "truncate 1 >c:\users\gaohongchen\desktop\45 \ 5 \13 ): Error C4244: "Initialize": from "float " to " Span style= "color: #0000ff;" >int ", data may be missing
#pragma comment: this directive places a note record into an object file or executable file.
The commonly used LIB keyword can help us to connect to a library file. For example:
#pragma comment (lib, "Wsock32.lib")
#pragma disable: Declared before a function, only valid for one function. The function call will not be interrupted in the process. Generally used in the C51 more.
Commonly used preprocessor directives for C + +