Conditional compilation (# ifdef # else # endif # If)

Source: Internet
Author: User

Time: 2014.07.01

Location: Base

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

I. Pre-processing details before compiling conditions, first understand the pre-processing process. The pre-processing process reads the source code, that is, scanning the source code, checking the contained pre-processing commands and macro definitions, performing a preliminary conversion on them, and then providing the generated new source code to the compiler. The pre-processing process also deletes comments and extra spaces in the program.
The pre-processing command starts with #, followed by the command keyword. The command will convert the source code before the compiler compiles the source code. Common Pre-processing Commands include:
# Empty instruction // No Effect
# Include <> // contains a source code file.
# Define // define a macro
# UNDEF // cancel the 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 given condition is not true and the current branch condition is met, compile the following code
# Endif // end one # If... # else condition compilation Block
# Error // stop compilation and Display Error Information
# Line // change the file number and line number used by the compiler to indicate warnings and error messages
# Pragma // The Custom purpose of the compiler. Generally, some annoying warning information is prohibited or allowed.

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

2. The file inclusion # include preprocessing command is used to expand the contained file in the command. This type of content contains multiple elements, that is, other files can be included in an included file. The Preprocessing process does not check whether a file is contained in the conversion unit and prevents its repeated (multiple times) Inclusion. Therefore, we can achieve different effects by specifying the compile-time conditions. For example:
To avoid repeated inclusion of only one header file, we can use the compile-time condition to control the header file. // My header file
//my_header.h#ifndef MY_HEADER_H#define MY_HEADER_H......#endif
How to include header files in a program
1. Use angle brackets to include, for example:
# Include <myheader. h>
This method tells the Preprocessor program to search for the contained header file in the header file of the compiler or external library.
2. Double quotation marks include, for example:
# Include "myheader. H"
This method tells the Preprocessor to search for the contained header file in the source code file of the currently compiled application. If no header file is found, search for the header file that comes with the compiler.

----------------------------------------------------------------------------
Iii. Conditional compilation
The Conditional compilation command determines which code is compiled and which code is not compiled, so that the code is executed in the program and where the code falls.
1. # If command
# The if command detects the compilation conditions (a constant expression) after the command. If it is true, the code after the command is compiled until # else, # Elif, or # endif appears. If it is false, it is not compiled.
2. # endif command
# Endif is used to terminate # If preprocessing command.
For example:

#define DEBUG 0int main(){  #if DEBUG   printf("Debugging");  #endif  printf("Running");}
Here, the program defines the debug macro to represent 0, so the # If condition is false, and the subsequent Code cannot be compiled until # endif, so the program directly outputs the running. If the # Defin statement is removed, the same is true.
3. # Elif command
# Elif pre-processing commands combine the functions of # else and # If commands
#define FLAG{  #ifdef ONE  print("1");  #elif defined TWO  printf("2");  #else  printf("3");  #endif}
Final output 2
Condition 1
# Ifdef XXXX... program segment 1... # Else... program segment 2... # Endif
It 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 a line such as # define num already exists at the beginning of the program, that is, num has been defined, when the # ifdef num below is met, the first printf will be executed, otherwise, if this line is not defined above, the second printf will be executed.
To sum up, you can easily enable or disable certain functions of the entire program.
2. Case 2
# Ifndef XXXX... program section 1 # else... program section 2 # endif
Currently, the # ifndef is used, that is, if the identifier XXXX is not defined, the program segment 1 will be compiled; otherwise, the program segment 2 will be compiled.
3. Case 2
# If constant... program section 1 # else... program section 2 # endif
This indicates that if the constant is true, the program segment 1 will be compiled; otherwise, the program segment 2 will be compiled.
In this way, you can add the test code. To enable the test, you only need to set the variable to 1, instead of the variable 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.