Preprocessing is the processing before compiling, and one of the tasks of compiling is grammar checking, preprocessing does not do grammar checking. The preprocessing command begins with the symbol "#".
Common preprocessing 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. A macro substitution is a substitution, not a calculation, or an expression solution. Macros define macro definitions with parameters and macro definitions with no parameters. In macro definitions with parameters, there can be no spaces between the parentheses of the macro name and the arguments.
The macro definition does not allocate memory, and the variable definition allocates memory.
Macro expansion does not account for elapsed time, only for compile time, and function calls for elapsed time (allocating memory, preserving locality, value passing, return value).
The # operator appearing in the definition of a macro converts the parameter followed by a string, sometimes using the # of this usage as a string operator. For example:
Copy Code code as follows:
#include <iostream>
using namespace Std;
#define STR (N) "ABCD" #n
int main ()
{
COUT<<STR (6) <<endl;
System ("pause");
return 0;
}
The output results are:
# #运算符用于把参数连接到一起, the preprocessor appears in the # #两侧的参数合并成一个符号. For example:
Copy Code code as follows:
#include <iostream>
using namespace Std;
#define STR (A,b,c) a# #b # #c
int main ()
{
Cout<<str (1,2,3) <<endl;
System ("pause");
return 0;
}
The output results are:
file contains
#include < file name > called the standard Way, to the system header file directory lookup file, #include "filename" 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> for example: Iostream.h is a C-language format header (library) file, for the old version of the standard library, only narrow character sets are supported , while iostream is a standard header file for C + +, which supports narrow character sets and wide character sets.
Static global variables in the included file are not declared in the include file.
Conditional compilation
Use conditional compilation to make the target program smaller and run less time.
#undef指令, which is used to delete predefined macro definitions, in the general form of: #undef宏替换名
Error message directives
#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. Its general form is: #error出错信息.
Copy Code code as follows:
#ifndef __cplusplus
#error This isn't 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 predefined identifiers in the compiler program.
The format is: #line number [filename], which can change the current line numbers and file names.
Copy Code code as follows:
#include <iostream>
using namespace Std;
#line "A.cpp"
int main ()
{
cout<<__line__<< ' t ' <<__FILE__<<endl;
System ("pause");
return 0;
}
Run Result:
Layout control directives
In all preprocessing instructions, the #Pragma instruction may be the most complex, and its role is to set the state of the compiler or instruct the compiler to perform certain actions. #pragma指令对每个编译器给出了一个方法, to give the host or operating system proprietary features, while maintaining complete 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 Compilation Information Output window
Copy Code code as follows:
#pragma messages (message text)
CODE_SEG parameter: Set the code snippet stored in the function code in the program, and use it when we develop the driver.
Copy Code code as follows:
#pragma code_seg (["Section-name" [, "Section-class"]])
#pragma once:
Adding this instruction at the very beginning of the header file ensures that the header file is compiled once, but poorly ported. If you are writing a program that is cross-platform, it is best to use the macro definition in C + +.
#pragma hdrstop: Indicates that the precompiled header file ends here, and the following header file is not precompiled.
#pragma resource:
Copy Code code as follows:
#pragma resource "WINFORM.DFM"
Indicates that the resource in the WINFORM.DFM file is added to the project, and the definition of the form's appearance is included in WINFORM.DFM.
#pragma warning: output warning message.
Let's Run the program:
Copy Code code as follows:
int main ()
{
float f=3.6;
int i=f;
cout<<i<<endl;
System ("pause");
return 0;
}
The following warning message appears:
Copy Code code as follows:
1>clcompile:1>f.cpp1>c:\users\gaohongchen\desktop\45\5\5\f.cpp (): Warning C4305: "Initialize": from "Double" to " Float truncate 1>c:\users\gaohongchen\desktop\45\5\5\f.cpp: Warning C4244: Initialize: Converting from float to int, possibly losing data
If we add the above program:
Copy Code code as follows:
#pragma warning (disable:4305)
The compile-time warning message changes to:
Copy Code code as follows:
1>clcompile:
1>f.cpp
1>c:\users\gaohongchen\desktop\45\5\5\f.cpp (): Warning C4305: "Initialize": from "double" to "float" truncated
1>c:\users\gaohongchen\desktop\45\5\5\f.cpp: Warning C4244: initialization: Converting from float to int, possibly losing data
If we add the above program:
Copy Code code as follows:
#pragma warning (error:4244)
The warning message becomes an error message:
Copy Code code as follows:
1>clcompile:
1>f.cpp
1>c:\users\gaohongchen\desktop\45\5\5\f.cpp: Warning C4244: initialization: Converting from float to int, possibly losing data
#pragma comment: This directive puts a note record in an object file or executable file.
The commonly used LIB keyword can help us to connect to a library file. For example:
Copy Code code as follows:
#pragma comment (lib, "Wsock32.lib")
#pragma disable: Declared before a function, valid only for one function. The function call procedure will not be interrupted. Generally used more in C51.