C #if 0---#endif和 #if 1---#endif

Source: Internet
Author: User

What is the effect of this sentence?

The condition is always false, used to skip some code snippets, to skip the code between #if 0 and #endif at compile time, which causes #if 0---#endif之间的语句不会被编译, which is a precompiled statement.

When commenting out chunks of code, using "#if 0" is better than using "/**/", because the annotation of "/**/" to the large section of the annotation to prevent the annotated code has nested "/**/", which will cause the comment out of the code area is not the scope you want, when the commented out of the big code is very easy to happen, This is especially true after a period of time when the code is modified.

     here's how to compile the conditional (#ifdef, #else, #endif, #if等). The following are divided into 3 cases:
1. Situation 1: 
#ifdef _xxxx ...
Program Section 1
... #else
...  Program Section 2
... #endif
This indicates that the program segment 1 is compiled if the identifier _xxxx has been defined by the #define command, otherwise the program segment 2 is compiled.  
Example: 
#define NUM
.......  .............
.............
num has been defined before #ifdef num printf.  :) \ n ");
#else
printf ("There is no definition before Num.  :(\ n ");
#endif
}
If the program has #define NUM at the beginning of this line, that is, NUM is defined, and when you encounter #ifdef num below, of course, the first printf is executed. Otherwise, the second printf will be executed.
I think, with this, you can easily turn on/off a specific feature of the entire program.
2: Condition 2: 
#ifndef _xxxx 
...  Program Section 1 
... #else 
...  Program Section 2 
... The
#ifndef is used here #endif to indicate if not def. It is, of course, the opposite of #ifdef (if the identifier _xxxx is not defined, then execution Segment 1 is executed, otherwise the Execution Program section 2). The example will not be lifted.
3: Condition 3:
#if constant 
...  Program Section 1
... #else
...  Program Section 2
... #endif 
Here, if the constant is true (not 0, whatever the number, as long as it is not 0), execute the program segment
1, otherwise the Execution section 2.
If there are #if need below to write


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.