#ifdef #else #endif The use of #if #ifndef

Source: Internet
Author: User

Preprocessing is the work done prior to the first lexical scan and parsing of the compilation. To be blunt, the preprocessing part is processed before the source file is compiled, and then the processed code is compiled. The advantage of this is that the processed code will become very short.
About the file contained in the preprocessing command (#include), the macro definition (#define), the book has a detailed description, here is not detailed. This is mainly a description of conditional compilation (#ifdef, #else, #endif, #if等). Here are 3 things:
  

1 preprocessing is the work done prior to the first lexical scan and parsing of the compilation. To be blunt, the preprocessing part is processed before the source file is compiled, and then the processed code is compiled. The advantage of this is that the processed code will become very short. 2About the file contained in the preprocessing command (#include), the macro definition (#define), the book has a detailed description, here is not detailed. This is mainly a description of conditional compilation (#ifdef, #else, #endif, #if等). Here are 3 things:3     1: Situation 1:4     #ifdef_xxxx5 ..... Program Section 1 ...6     #else7 ..... Program Section 2 ...8     #endif9 This indicates that if the identifier _xxxx has been defined by the # define command, the program Segment 1 is compiled, otherwise the program segment 2 is compiled. Ten Example: One     #defineNum A ... ..... - ... ..... - ... ..... the     #ifdefNUM -printf"num has been defined before! :) \ n"); -     #else -printf"Num has never been defined before! :(\ n"); +     #endif - } +If the program starts with#defineNUM, which is defined by NUM, will, of course, execute the first printf when it encounters the #ifdef num below. Otherwise a second printf will be executed. AI think, with this, can be very convenient to open/turns off a specific feature for the entire program.  at     2: Situation 2: - #ifndef _xxxx - ..... Program Section 1 ... -     #else  - ..... Program Section 2 ... -     #endif in The #ifndef is used here, which means if not def. Of course it is the opposite of #ifdef (if the identifier _xxxx is not defined, then execute program segment 1, otherwise execute program segment 2). The example is not to be lifted.  -     3: Situation 3: to     #ifConstant + ..... Program Section 1 ... -     #else the ..... Program Section 2 ... *     #endif  $ This indicates that if the constant is true (not 0, whatever the number, as long as it is not 0), execute program segment 1, otherwise execute program segment 2. Panax NotoginsengIn my opinion, this method can add the test code. When you need to open the test, as long as the constant quantity of 1 is good. Instead of testing the time, as long as the constant quantitative 0.

We mainly use the following methods, assuming that we have defined #ifdef Debug and #ifdef TEST in the program header:

1. Use #ifdef/#endif to include a program function module in order to provide this function to a user.

In the program header definition #ifdef HNLD:

#ifdef HNLD

# include "N166_hn.c"

#endif

If you are not allowed to provide this functionality to other users, add a hnld to the header before compiling.

2. Precede each sub-program with a tag to track the operation of the program.

#ifdef DEBUG

printf ("Now's in Hunan!");

#endif

3. Avoid hardware limitations. Sometimes some specific application environment hardware is different, but limited to the condition, the local lack of such equipment, and then bypass the hardware, directly write the expected results. The specific approach is:

#ifndef TEST

I=dial ();

Program debugging run-time bypass this statement

#else

i=0;

#endif

After debugging passes, the definition of test is masked and recompiled, which can be sent to the user for use.

This article turns from csdn lijinyan3000 http://bbs.csdn.net/topics/210046082

For individual learning and use

#ifdef #else #endif The use of #if #ifndef

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.