C language 16-preprocessing directive 2-conditional compilation

Source: Internet
Author: User

    • Concept of conditional compilation
    • First, basic usage
    • Two, give an example
    • Third, Other uses

Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are not interested in C or iOS development, please ignore.

The previous article has introduced the macro definition in the preprocessing directive, this article introduces the conditional compilation

Concept of conditional compilation

In many cases, we want some of the code in the program to compile only if certain conditions are met, otherwise it does not participate in compilation (only the code that participates in the compilation will eventually be executed), which is conditional compilation .

First, basic usage
1 #if conditions ...  code1 ... 3 #elif conditions ...  code2 ... 5 #else6 ...  code3 7 #endif

1> If condition 1 is set up, then the compiler will put # if and #elif之间的code1代码编译进去(note: is compiled into, not executed, very ordinary if-else is not the same)
2> If condition 1 is not established and condition 2 is set up, then the compiler will #elif and #else之间的code2代码编译进去

3> if conditions 1 and 2 are not true, then the compiler will #else and #endif之间的code3编译进去

4> Note that after the conditional compilation ends, add a #endif to the last face, or the consequences are serious (think about the consequences yourself)

5> #if and #elif后面的条件一般是判断宏定义而不是判断变量, because conditional compilation is a judgment done before compilation, and a macro definition is defined before compilation, and the variable is generated at run time, meaning it is used.

Two, give an example
1 #include <stdio.h> 2  3 #define MAX 4  5 int main () 6 {7 #if max = = 0 8     printf ("MAX is 0"); 9 #elif MA X > 010     printf ("Max is greater than 0"), one #else12     printf ("Max is less than 0"), #endif14     return 0;15}

In line 3rd, you define a macro max, which, of course, may be defined in the other header file in development, and is now written to the main function for illustrative purposes only. Note the conditional compilation statements for lines 7th through 13th.
Since Max is 11, the #elif condition is set, and the 10th line of code will be compiled, in fact, after the pre-processing of the compiler code is this:

The contents of the 1/*stdio.h file will replace the location of # include <stdio.h> */2 3 int main () 4 {5     printf ("Max is greater than 0"), 6     return 0;7}

The code becomes very concise and the output is:

Third, Other uses

1. Usage of #if defined () and # if!defined ()

#if and #elif后面的条件不仅仅可以用来判断宏的值, you can also determine whether a macro has been defined. Like what:

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

If you have already defined Max as a macro, compile code into it. It does not control the value of Max, as long as Max is defined, the condition is set.

Conditions can also be reversed:

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

If Max is not defined previously, the code is compiled in.

2. Use of #ifdef和 #ifndef

* The usage of #ifdef的使用和 # if defined () is basically consistent

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

If you have already defined Max as a macro, compile code into it.

* The usage of #ifndef又和 # if!defined () is basically consistent

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

If Max is not defined previously, the code is compiled in.

C language 16-preprocessing directive 2-conditional compilation

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.