Pre-compilation directives related to conditional compilation in C language

Source: Internet
Author: User
Tags constant definition

I. Overview of the Content

This article mainly introduces the pre-compiling directives related to conditional compilation in C language, including # define, #undef, #ifdef, #ifndef, #if, #elif, #else, #endif, defined.

Second, conditional compilation

Conditional compilation is the means by which code is statically compiled based on the actual definition of a macro (a certain type of condition). A compilation condition can be determined based on the value of an expression or whether a particular macro is defined.

The most common conditional compilation is to prevent macros containing header files from repeating, similar to the following code:

1 #ifndef Abcd_h 2 #define Abcd_h34//  ... some declaration codes56#endif // #ifndef Abcd_h

In an implementation file, there is usually a definition similar to the following:

1 #ifdef _DEBUG 2 3 // ... do some operations 4 5 #endif 6 7 #ifdef _WIN32 8 9 // . ... use   Win32 API#endif

These are common scenarios for conditional compilation.

Iii. pre-compiled directives used in conditional compilation

#define Define a preprocessing macro
#undef canceling the definition of a macro

#if conditional commands in compilation preprocessing, equivalent to the IF statement in C syntax
#ifdef Determine if a macro is defined, and if it is defined, executes the subsequent statement
#ifndef In contrast to #ifdef, determine if a macro has not been defined
#elif If # If, #ifdef, #ifndef或前面的 the #elif condition is not met, the statement after #elif is executed, equivalent to the else-if in C syntax
#else and # If, #ifdef, #ifndef对应, if these conditions are not met, the statement after #else is executed, equivalent to the else in C syntax
#endif #if, #ifdef, #ifndef这些条件命令的结束标志.
Defined and # If, #elif配合使用 to determine if a macro is defined

D. Pre-compiled instructions application Example 1. #define, #undef

#define命令定义一个宏:
#define macro_name[(args)] [tokens[(opt)]]
Subsequent occurrences of the macro_name will be substituted for the defined tag (tokens). Macros are available with parameters, and subsequent markers are optional.

Macro definition, according to whether the parameter is usually divided into object macro, function macro two kinds.
Object macros: Macros without parameters are referred to as "Object macros (Objectlike macro)". Object macros are used to define constants, common identities. For example:

// constant Definition #define Max_length//  Universal ID, log output macro #define SLog printf//  Precompiled Macro #define _ DEBUG

Function macro: A macro with parameters. The use of macros can improve the efficiency of the code: subroutine calls need to stack out the stack, this process if too often consumes a lot of CPU computing resources. So some code that is small but runs frequently can improve the efficiency of your code if you implement it with a parameter macro. However, most C + + programs do not recommend the use of function macros, debugging a certain degree of difficulty, you can consider using C + + inline instead. For example:

// Minimum value function #define MIN (a) > (b)? (a):(B))//  Safe release memory function #define safe_delete (p) {if (null!=p) {DELETE p; p = NULL;}}

#undef可以取消宏定义, corresponding to # define.

2. Defined

Defined is used to test whether a macro is defined. Defined (name): Wakahiro is defined, returns 1, otherwise returns 0.
It is with # if, #elif, #else结合使用来判断宏是否被定义, at first glance as if it seems superfluous, as there are already #ifdef and #ifndef. Defined can be used to declare multiple discriminant conditions in a judgment statement; #ifdef和 #ifndef only supports determining whether a macro is defined.

#if

and # If, #elif, #else不同, #ifdef, #ifndef, defined the macro that you test can be either an object macro or a function macro.

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

A relatively common precompiled directive in conditional compilation. The pattern is as follows:

#ifdef ABC // ... codes while definded ABC #elif (Code_version > 2) // ... codes while code_version > 2 #else // ... remained cases #endif //

#ifdef用于判断某个宏是否定义, in contrast to the #ifndef function, they only support judging whether a single macro has been defined, which can be interchanged in the example above. If you do not require multi-conditional precompilation, both the #elif and #else in the above example can be written.

4. #if, #elif, #else, #endif

#if可支持同时判断多个宏的存在, used in conjunction with a constant expression. Common formats are 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, and if the constant expression is an undefined macro, its value is treated as 0.

#if // equivalent to #if 0

You should avoid using # if when judging whether a macro is defined, because the value of the macro may be defined as 0. Instead, #ifdef or #ifndef should be used.
Note: #if, #elif之后的宏只能是对象宏. If the macro is undefined, or if the macro is a function macro, the compiler may have a warning that the macro is undefined.

V. Summary

This article focuses on the instructions in the C language about precompilation. The purpose of this article is to clarify the relevant concept calls and to find the most appropriate instruction and format for subsequent precompilation. For example, to meet the multiple macro definitions of pre-compilation, multi-branch precompilation, #elif和 #else instructions and so on.

Resources:

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

Pre-compilation directives related to conditional compilation in C language

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.