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 # operator that appears in the macro definition converts the argument that follows it to a string, and sometimes the # of this usage is called the string operator. For example:
#include <iostream>using namespace std, #define STR (n) "ABCD" #nint Main () { cout<<str (6) <<endl; System ("pause"); return 0;}
The output is:
# #运算符用于把参数连接到一起, the preprocessor appears in the # #两侧的参数合并成一个符号. For example:
#include <iostream>using namespace std; #define STR (A,b,c) a# #b # #cint Main () { cout<<str (<) <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 the general form: #undef宏替换名
Error message directive
#error指令, the instruction is used for debugging the program, outputting an error message, and stopping compiling when the #error instruction is encountered in the compilation. The general form is: #error出错信息.
#ifndef __cplusplus#error This was not a C + + complier. #endif #include<iostream>using namespace Std;int main () { System ("pause"); return 0;}
#line指令
The command #line changes 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>using namespace 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: Warning C4305: Initialize: From the Double "to" float "truncation 1>c:\users\gaohongchen\desktop\45\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.cpp
1>c:\users\gaohongchen\desktop\45\5\5\f.cpp: Warning C4305: "Initialize": Truncate from "Double" to "float"
1>c:\users\gaohongchen\desktop\45\5\5\f.cpp: Warning C4244: "Initialize": Convert from "float" to "int", possibly losing data
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\5\f.cpp: Warning C4244: "Initialize": Convert from "float" to "int", possibly losing data
#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.
General Instructions for C + + preprocessing