C language-related pre-compilation instructions and compilation instructions

Source: Internet
Author: User
Tags constant definition

C language-related pre-compilation instructions and compilation instructions
I. Content Overview

This article mainly introduces the pre-compiled commands related to Conditional compilation in C language, including # define, # undef, # ifdef, # ifndef, # if, # elif, # else, # endif, and defined.

Ii. Conditional compilation

Conditional compilation is a method for static code compilation based on the actual definition of macros (certain conditions. You can determine the compilation conditions based on the expression value or whether a specific macro is defined.

The most common condition for compilation is to prevent repeated Macros containing header files in the same form as the following code:

1 #ifndef ABCD_H
2 #define ABCD_H
3 
4 // ... some declaration codes
5 
6 #endif // #ifndef ABCD_H

The following definitions are common in implementation files:

vThese are common scenarios for Conditional compilation.
Iii. precompiled commands used in Conditional compilation

# Define defines a preprocessing macro
# Undef cancel macro definition

# If compile the conditional command in preprocessing, which is equivalent to the if statement in C syntax
# Ifdef determines whether a macro is defined. If so, execute the subsequent statement.
# Opposite to # ifdef, ifndef determines whether a macro is not defined.
# If the # if, # ifdef, # ifndef or the # elif condition does not meet the preceding conditions, the statement after # elif is executed, which is equivalent to the else-if statement in C syntax.
# Else corresponds to # if, # ifdef, # ifndef. if these conditions are not met, run the statement after # else, which is equivalent to the else in C syntax.
# Endif # if, # ifdef, # ifndef end flag of these conditional commands.
Defined and # if, # elif are used together to determine whether a macro is defined

Iv. Example of precompiled command Application 1. # define, # undef

# The define command defines a macro:
# Define MACRO_NAME [(args)] [tokens [(opt)]
MACRO_NAME will be replaced with the defined tag (tokens ). A macro can contain parameters, and the following mark is optional.

Macro definition, usually divided into two types: Object macro and function macro.
Object macro: a macro without parameters is called "objectlike macro )". Object macros are used to define constants and general identifiers. For example:

// constant definition
#define MAX_LENGTH 100
// General logo, log output macro
#define SLog printf
// pre-compiled macros
#define _DEBUG

FUNCTION macro: a macro with parameters. Macro can improve the code running efficiency: the calling of subprograms requires the stack to be pushed out of the stack. If this process is too frequent, it will consume a large amount of CPU computing resources. Therefore, if some code with a small amount of code but frequently run code is implemented with a parameter macro, the code running efficiency will be improved. However, function macros are not recommended for most c ++ programs. debugging is difficult. You can use the inline of c ++ instead. For example:

// minimum function
#define MIN (a, b) ((a)> (b)? (a) :( b))
// Safely release memory function
#define SAFE_DELETE (p) {if (NULL! = p) {delete p; p = NULL;}}

# Undef can cancel the macro definition, which corresponds to # define.

2. defined

Defined is used to test whether a macro is defined. Defined (name): If the macro is defined, 1 is returned; otherwise, 0 is returned.
It is used in combination with # if, # elif, and # else to determine whether a macro is defined. At first glance, it seems redundant because it already has # ifdef and # ifndef. Defined can be used to declare Multiple Discriminant conditions in a judgment statement; # ifdef and # ifndef can only determine whether a macro is defined.

#if defined(VAX) && defined(UNIX) && !defined(DEBUG) 

Unlike # if, # elif, and # else, # ifdef, # ifndef, and defined macros can be object macros or function macros.

3. # ifdef, # ifndef, # else, # endif

Precompiled commands that are commonly used in Conditional compilation. The mode is as follows:

v# Ifdef is used to determine whether a macro is defined. The function is opposite to the function of # ifndef. The two can only determine whether a single macro has been defined. In the above example, the two can be exchanged. If multi-condition pre-compilation is not required, # elif and # else in the above example can be left empty.
4. # if, # elif, # else, # endif

# If can be used together with constant expressions to determine the existence of multiple macros at the same time. The common format is as follows:

#if Constant expression 1
// ... some codes
#elif Constant expression 2
// ... other codes
#elif Constant expression 3
// ...
...
#else
// ... statement
#endif 

A constant expression can be a valid C constant expression that contains macros, arithmetic operations, logical operations, and so on. If a constant expression is an undefined macro, its value is regarded as 0.

# If MACRO_NON_DEFINED // equivalent to # if 0

# If should be avoided when determining whether a macro is defined, because the macro value may be defined as 0. # Ifdef or # ifndef should be used.
Note: The macros after # if and # elif can only be object macros. If the macro is undefined, or the macro is a function macro, the compiler may have a warning that the macro is undefined.

V. Summary

This section describes the pre-compiled commands in C. The purpose of this article is to clarify the calling of related concepts and find the most appropriate command and format in the future pre-compilation. For example, it can meet the requirements of Multi-macro pre-compilation, multi-branch pre-compilation, # elif and # else commands at the same time.

References:

Preprocessor Directives http://msdn.microsoft.com/zh-cn/library/aa381033


Which of the following three types of pre-compilation commands are provided in C language?

1. macro definition: Use a specified identifier (name) to represent a string. For example, use PI to represent 3.1415926, # define PI 3.1415926
2. File Inclusion: A source file can contain all the content of another source file. # include <File Name>
3. Conditional compilation: Specify the compilation conditions for a part of the content, that is, compile only when certain conditions are met, mainly including:
(1) # ifdef identifier
Procedure 1
# Eles
Procedure 2
# Endif
(2) # ifndef identifier
Procedure 1
# Eles
Procedure 2
# Endif
(3) # if identifier
Procedure 1
# Eles
Procedure 2
# Endif

What is the pre-compiled command table in C language?

1 macro definition is string replacement. Macros are classified into non-parametric macros and parametric macros. The location of the macro can be either outside the function or inside the function (tested by vc ++ 2008 ). The macro scope is the part between the definition and the undefined Command [# undef macro name]. If no explicit # undef command exists, the file ends by default. You can use the defined command to determine whether a macro is defined. # if defined X (= # ifdef X), # if! Defined X (= ifndef X ). Define the macro format with parameters such as # define SQ (y) * (y). The parameter is y, and the macro is y square. To ensure the correctness of macro replacement, three parentheses are added. However, this guarantee is also limited. It requires that the value of y cannot be changed within (y). For example, changing y to I ++ will not produce the expected results. Macro call (in fact, macro replacement) does not need to consider the type of the form parameter, which brings some benefits. For example, the macro that calculates the two maximum values # define MAX (a, B) (a> B )? A: B. The real parameter can be int or double. Macro definition can include multiple statements, such as # define CHANGE (X1, X2, X3, X4) X1 + = 1; X2 + = 2; X3 + = 3; X4 + = 4; 2. The file inclusion command # include function is used to insert a specified file into the command line to replace the command line, so that the specified file and the current source file are connected into a source file. When the source code is distributed across multiple files, we recommend that you use the call file + header file + implementation file mode. The header file contains the function description, type definition, macro definition, and constant value. The specific implementation is placed in the implementation file. This header file is included in the call file and implementation file. To avoid repeated header files, you can use the # ifndef [header file identifier (such as X_Header)] + header file content + # endif mode in the header file. 3. The Conditional compilation command can compile different program parts according to different conditions, thus generating different target code files. This is useful for program porting and debugging. Conditional compilation has three forms:
The first form selects the Compilation Program Segment Based on the macro definition:
# Ifdef identifier // or # ifndef identifier
Procedure 1
# Else
Procedure 2
# Endif the second form selects compilation based on the constant expression value. The value is not 0 and the if segment is executed. # If constant expression
Procedure 1
# Else
Procedure 2
# Endif
Here it must be a constant expression, which is generally a macro. If the expression contains variables, the compiler can only guess one. The third form contains # elif. Let's take a look at the example # define ABC 3
Void main () {# if ABC> 0
Int a = 1;
Printf ("% d/n", );
# Elif ABC <0
Int B =-1;
Printf ("% d/n", B );
# Else
Int c = 0;
Printf ("% d/n", c );
# Endif
} The remaining pre-compiled commands are as follows. I will not study them here. # Line indicates the row number of the statement
# Replace a macro parameter with a character escape constant whose value is the content
# Connect two adjacent tokens into a separate tag
# Pragma indicates compiler information # warning displays compilation warning information
# Error show compilation error information
 

Related Article

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.