Preprocessing directives provide the ability to conditionally skip sections in source files, report errors and warning conditions, and delineate different areas of source code. The term "preprocessing directives" is used only for consistency with the C and C + + programming languages. There is no separate preprocessing step in C #, and preprocessing directives are processed as part of the lexical analysis phase.
The primary function of the preprocessor is to replace the one resource with the built-in function of preprocessing, the most common preprocessing is: file contains, conditional compilation, layout control and macro substitution 4 kinds.
File contains: #include is one of the most common preprocessing, mainly as a reference to the file combination source program body.
Conditional compilation: #if, #ifndef, #ifdef, #endif, #undef等也是比较常见的预处理, mainly compile-time selective selection, comment out some of the specified code to achieve version control, to prevent duplication of the file containing functionality.
Layout control: #progma, which is an important aspect of our application preprocessing, the main function is to provide the compiler with unconventional control flow information.
Macro substitution: #define, which is the most common use, it can define various functions such as symbolic constants, function functions, renaming, concatenation of strings, and so on.
Let's look at the common preprocessing directives:
#define MACRO Definition
#undef undefined macro
#include text contains
#ifdef compile
If the macro is defined #ifndef if the macro is not defined, compile
#e NDIF end the control of the compilation block
#if the expression to compile without zero
#else compile as the remaining option for other preprocessing
#elif This is a combination of #else and # # options
#line Change the current number of rows and File name
#error output an error message
#pragma to provide the compiler with unconventional control flow information
below we explain one by one of these preprocessing, taking into account the importance and complexity of the macro, we put it to the last.
file contains directives:
This preprocessing is the most common use, usually we write programs will be used, the most common use is:
#include <iostream>//Standard library header file
#include <ios Tream.h>//Legacy standard library header file
#include "IO.h"//user-Defined header file
#include ". /file.h the header file under the parent directory under//unix
#include the full path under/usr/local/file.h//unix
#include. \file.h "header file under parent directory under//dos
#include full path under" \usr\local\file.h "//dos
There are 2 places to be aware of:
1, we use <iostream> or <iostream.h>
We advocate the use of <iostream>, rather than <iostream.h> I think you may remember that I have given a few reasons, here I would like to say: First, the. h format header files were abandoned by the standard Committee as early as September 98, and we should follow the standards to suit the development of the Times. Second, Iostream.h supports only narrow character sets, and iostream supports narrow/wide character sets.
Also, the standard has made a lot of changes to iostream, and the interface and implementation have changed. Finally, the iostream components are all put into namespace Std to prevent name contamination. [NextPage]
2, <io.h> and "io.h" the difference?
In fact, their only difference is that the search path is different:
For # include <io.h>, the compiler starts searching from the standard library path
For # include "IO.h", the compiler starts searching from the user's work path
Compile control directives:
The main purpose of these directives is to compile-time selective selection, commenting out some of the specified code to achieve version control, to prevent duplication of the file containing functionality.
Use the format as follows:
If the identifier is a defined symbol, your code is compiled, otherwise it is excluded
2.
If the identifier is an undefined symbol, your code is compiled, otherwise it is excluded
3.
If expression is nonzero, your code is compiled or rejected
4.
If identifier is a defined symbol, your code1 will be compiled, otherwise yourcode2 will be compiled
5.
If Epression1 is nonzero, compile your code1, otherwise, if expression2 nonzero, compile your code2, otherwise compile your code3
Other pre-compiled directives
In addition to the above-mentioned compilation instructions, there are 3 less common compilation directives: #line, #error, #pragma, we'll talk about it briefly.
#line的语法如下:
For example: #line a.h where the file name A.h can be omitted from writing.
This instruction can change the current line number and file name, such as the above preprocessing instruction can change the current line number is 30, the file name is A.H. At first glance seems to be no use, however, he still a bit of use, that is used in the compiler, we know that the compiler in C + + source code will produce some intermediate files, through this directive, can guarantee that the file name is fixed, will not be replaced by these intermediate files, conducive to analysis.
For example:
This instruction is mainly to give the error message, the above example is, if not in the UNIX environment, will output this software requires the Unix OS. Then induce the compiler to terminate. So in general, the purpose of this instruction is to give certain information before the program crashes.
#pragma是非统一的, he relies on each compiler producer, for example, in the Sun C + + compiler:
Pre-defined identifiers
To handle some useful information, preprocessing defines some preprocessing identifiers, although the various compilers have different preprocessing identifiers, but they will handle the following 4 types:
1. __file__ the name of the file being compiled
2. __line__ the line number of the file being compiled
3. __date__ date string for compile time, for example: "Dec 2000"
4. __time__ time string for compile time, for example: "12:30:55"
For example:cout<< "The file is:" <<__file__ "<<"! The lines is: "<<__LINE__<<endl;
Where to Preprocess
How to replace the # include pre-processing directives, we are no longer one by one discussed here.
C + + does not provide an alternative form of # include, but namespace provides a scope mechanism that can support the combination in some way, using it to improve the behavior of # include, but we still cannot replace # include.
#progma应该算是一个可有可无的预处理指令, according to C + + 's father Bjarne, is: "#progma被过分的经常的用于将语言语义的变形隐藏到编译系统里, or be used to provide language extensions with special semantics and clumsy syntax. ”
For #ifdef, we are still helpless, even if we use the IF statement and the constant expression, still not enough to replace her, because the body of an if statement must be syntactically correct, to satisfy the class check, even if he is in a branch that will never be executed.
Summary of pre-compiled directives in C/