16-c language preprocessing Directive 2-conditional compilation

Source: Internet
Author: User

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

#if Condition 1 ... code1 #elif Condition 2 ... code2 #else ... .. code3 ... #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

#include <stdio.h>#defineMAX 11intMain () {#ifMAX = = 0printf ("Max is 0 .");#elifMAX > 0printf ("Max is greater than 0");#elseprintf ("Max is less than 0");#endif    return 0;}

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 stdio.h file will replace the location of # include <stdio.h> */ int Main () {    printf ("Max is greater than 0");     return 0 ;}

The code becomes very concise and the output is:

Iii. other usage 1. Use 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.

16-c language 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.