Edit Article-Blog channel-csdn.net

Source: Internet
Author: User

Date: 2014.07.01

Location: Base

----------------------------------------------------------------------------

First, pretreatmentUnderstand the pre-processing process before compiling the conditions. The preprocessing process reads the source code, that is, to scan the source code, check the included preprocessing instructions and macro definitions,to make a preliminary conversion,The resulting source code is then provided to the compiler. The preprocessing process also removes comments and extra white-space characters from the program.
The preprocessing directives begin with the # sign followed by the instruction keyword, and the instruction will do some conversion to the source code before the compiler compiles the source code. The common preprocessing directives are as follows:
#空指令//No effect
#include <>//include a source file
#define//define a macro
#undef//Cancel a defined macro
#if//If the given condition is true, compile the following code
#ifdef//If the macro is not defined, compile the following code
#ifndef//If the macro is not defined, compile the following code
#elif//If the previous # if condition is not true and the current branch condition is met, the following code is compiled
#endif//End an # if ... #else条件编译块
#error//Stop compiling and display error messages
#line//Change the file number and line number used by the compiler to indicate warning and error messages
#pragma//compiler custom purpose, generally prohibit or allow some annoying warning message

----------------------------------------------------------------------------

Second, the document contains#include预处理指令的功能是在指令处展开被包含的文件. This inclusion is multiple, which can include other files in a contained file. The preprocessing process does not check whether a file is already contained in the conversion unit and prevents it from being included repeatedly (multiple times). Therefore, we can achieve different effects by given compile-time conditions. Like what:
In order to avoid the inclusion of header files that can only be included once, we can control them in the header file with compile-time conditions. My header file
My_header.h#ifndef my_header_h#define my_header_h ... #endif
How to include header files in your program
1. Use angle brackets to include, as follows:
#include <myheader.h>
This tells the preprocessor program to search for the included header file in the header file of the compiler's own or external library.
2. Use double quotation marks to include, as follows:
#include "Myheader.h"
This method tells the preprocessor program to search for the included header file in the source code file of the currently compiled application, and then search the compiler's own header file if it is not found.

----------------------------------------------------------------------------

third, conditional compilationThe conditional compilation directive will determine which code is compiled and which code is not compiled, and what code will be executed in the program, and in what terms.
1. #if指令
#if指令检测后面的编译条件 (a constant expression), the following code is compiled for true until the #else, #elif或 #endif appear. It is not compiled for false.
2. #endif指令
#endif用于终止 # if preprocessing directives.
Like what:

#define DEBUG 0int Main () {  #if debug   printf ("debugging");  #endif  printf ("Running");}
Since this program defines the debug macro to represent 0, the # if condition is false and cannot compile the subsequent code until #endif, so the program outputs the running directly. The same is true if you remove the #defin statement.
3. #elif指令
Features of the #elif预处理指令综合了 #else and # if directives
#define Flag{  #ifdef one  print ("1");  #elif defined  -printf ("2");  #else  printf ("3");  #endif}
Final Output 2
Example 1:
#ifdef XXXX ... Program Section 1 ... #else ... Program Section 2 ... #endif
Indicates that if the identifier XXXX has been defined by the # define command, the program Segment 1 is compiled, otherwise the program segment 2 is compiled.
For example:
#define NUM ... #ifdef numprintf ("is already define NUM"), #elseprintf ("is not define NUM"); #endif

if the program starts with a line such as # define NUM, that is, NUM is already defined, then the first printf is executed when the following #ifdef NUM is encountered, otherwise the second printf is executed if there is no such line definition.
To sum up: In this way, it is very convenient to open and close certain functions of the whole program
Example 2:
#ifndef XXXX ... Program Segment 1#else ... Program Segment 2#ENDIF
Now using #ifndef, that is, if the identifier XXXX is not defined, then the program Segment 1 will be compiled, or the program segment 2
Example 3:
#if Constants ... Program Segment 1#else ... Program Segment 2#ENDIF
This means that if the constant is true, the program Segment 1 is compiled, otherwise the program segment 2 is compiled.
This way, this method can add the test code, when the test needs to be turned on as long as the variable is set to 1, and do not need to test the variable is set to 0




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.