# Ifdef Conditional compilation is actually so simple,

Source: Internet
Author: User

# Ifdef Conditional compilation is actually so simple,

Read a question today:

How can I eliminate the errors caused by multiple include headers?

The solution is to use Conditional compilation. For how to solve the "symbol redefinition error caused by multiple include headers", refer to blog


Only Conditional compilation is described as follows:

These macros are used for Conditional compilation. Generally, all the rows in the source program are compiled. However, sometimes you want to compile a part of the content only when certain conditions are met, that is, to specify the compilation conditions for a part of the content. This is "Conditional compilation ". Sometimes, you want to compile a group of statements when a condition is met, and compile another group of statements when the condition is not met.

It is used to compile program segment 1 when the identifier has been defined (generally defined using the # define command). Otherwise, compile program segment 2. The # else part does not exist.


If # idefine identifier (corresponding identifier) appears before # ifdef, run the program segment 1 for a long time; otherwise, run the program segment 2. The following simple example is written by myself:

Compile the code. Only the # else To # endif part will be compiled, and the running result will be output only: runing else part.

When # define debug is added to the sixth line, only the # ifdef to # else section will be compiled during Conditional compilation. Running result: runing main part

Advantages of Conditional compilation:

Some people may ask: what are the advantages of using Conditional compilation commands to directly use if statements without Conditional compilation commands? Indeed, this problem can be solved without the need for Conditional compilation. However, if the target program is long (because all statements are compiled), Conditional compilation can reduce compiled statements, this reduces the length of the target. When there are many Conditional compilation segments, the length of the target program can be greatly reduced.


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.