[C Language] 16-preprocessing command 2-Conditional compilation

Source: Internet
Author: User

Note: This C language topic is a prelude to iOS development. To enable programmers with object-oriented language development experience to quickly get started with the C language. If you have no programming experience or are not interested in C and iOS development, ignore

The previous article has introduced macro definitions in Preprocessing commands. This article introduces Conditional compilation.

Concept of Conditional compilation

In many cases, we hope that some of the code of the program can be compiled only when certain conditions are met. Otherwise, it will not be involved in compilation (only the Code involved in compilation can be executed eventually ).Conditional compilation.

I. Basic usage
1 # if condition 12... code1.... 3 # elif condition 24... code2. .. 5 # else6... code3.. 7 # endif

1> if condition 1 is true, the compiler will compile the code1 code between # if and # elif.(Note: It is compiled, not executed. The if-else is usually different)
2> If condition 1 is not true and condition 2 is true, the compiler will compile the code2 code between # elif and # else.

3> if conditions 1 and 2 are not true, the compiler will compile code3 between # else and # endif.

4> Note: After the condition compilation is complete, add a # endif at the end. Otherwise, the consequences will be very serious (think about the consequences yourself)

5> # if and # The conditions after elif are generally macro definitions rather than variables, because Conditional compilation is performed before compilation and macro definitions are defined before compilation, variables are generated at runtime and used only.

 

Ii. Example
1 # include <stdio. h> 2 3 # define MAX 11 4 5 int main () 6 {7 # if MAX = 0 8 printf ("MAX is 0 "); 9 # elif MAX> 010 printf ("MAX greater than 0"); 11 # else12 printf ("MAX less than 0"); 13 # endif14 return 0; 15}

A macro MAX is defined in row 3rd. Of course, this MAX may be defined in other header files during development. Now it is written to the main function for convenience of demonstration. Pay attention to the Conditional compilation statements from 7th to 13th rows.
Because MAX is 11, the # elif condition is true. The 10th lines of code will be compiled. In fact, the pre-compiled code is as follows:

1/* stdio. the content in the hfile will replace # include <stdio. h> position */2 3 int main () 4 {5 printf ("MAX greater than 0"); 6 return 0; 7}

The Code becomes very concise and the output result is:

 

Iii. Other usage 1. # if defined () and # if! Usage of defined ()

# If and # conditions after elif can be used not only to determine the macro value, but also to determine whether a macro has been defined. For example:

1 #if defined(MAX)2     ...code...3 #endif

If you have defined the MAX macro, compile the code. It does not care about the value of MAX. As long as MAX is defined, the condition is true.

The condition can also be reversed:

1 #if !defined(MAX)2     ...code...3 #endif

If the MAX macro has not been defined before, compile the code.

 

2. # Use of ifdef and # ifndef

* # The use of ifdef is basically the same as that of # if defined ()

1 #ifdef MAX2     ...code...3 #endif

If you have defined the MAX macro, compile the code.

* # Ifndef and # if! The usage of defined () is basically the same

1 #ifndef MAX2     ...code...3 #endif

If the MAX macro has not been defined before, compile the code.

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.