"Learning Notes", "C Language" conditional compilation

Source: Internet
Author: User

1. The 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.

2. Basic usage

#if conditions 1
..... code1 ...
#elif Conditions 2
..... code2 ...
#else
..... code3 ...
#endif

1> If condition 1 is set up, then the compiler will have # if and #elif之间的code1代码编译进去 (note: is compiled into, not execution, very often used if-else is not the same)?2> if the condition 1 is not established, the condition 2 is established, then the compiler will put the # 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.

3. Other uses

Usage of 1> #if defined () and # if!defined ()
#if and #elif后面的条件不仅仅可以用来判断宏的值, you can also determine whether a macro has been defined.
Like what:
#if defined (MAX)
.... Code ...
#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:
#if!defined (MAX)
.... Code ...
#endif
If Max is not defined previously, the code is compiled in.

Use of 2> #ifdef和 #ifndef

The use of #ifdef的使用和 # if defined () is basically consistent
#ifdef MAX
.... Code ...
#endif
If you have already defined Max as a macro, compile code into it.
* The usage of #ifndef又和 # if!defined () is basically consistent
#ifndef MAX
.... Code ...
#endif
If Max is not defined previously, the code is compiled in.

4. Code

1#include <stdio.h>2 3 //as long as you write # If, you must add #endif on the last side4 5 //#define A 56 7 intMain ()8 {9 #ifndef ATen //#ifdef A One //#if!defined (A) Aprintf"haha \ n"); - #endif -      the     //int a = ten; -     /* - if (a = = Ten) -     { + printf ("A is 10\n"); -     } + else if (a = = 5) A     { at printf ("A is 5\n"); -     } - Else -     { - printf ("A other value \ n"); -     }*/ in     /* -      to #if (A = = ten) + printf ("A is 10\n"); - #elif (A = = 5) the printf ("A is 5\n"); * #else $ printf ("A other value \ n");Panax Notoginseng #endif -       the      */ +      A     return 0; the}

"Learning Notes", "C Language" conditional compilation

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.