17-c language preprocessing Directive 3-file contains

Source: Internet
Author: User

Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are not interested in C or iOS development, please ignore

This is about the last preprocessing directive---file contains

First, the basic concept

Actually, we had a contact. The file contains this directive, which is # include, which can copy the entire contents of a file into another file.

Ii. general form 1.1th form # include < file name >

Find files directly in the directory where the C-language library function header files are located

2.2nd form #include "file name"

The system will look in the current directory of the source program, if not found, and then to the operating system path path to find, and finally to the C library function header file in the same directory to find

Third, the use of attention

1. #include指令允许嵌套包含, for example, A.H contains b.h,b.h containing c.h, but does not allow recursive inclusion, such as A.H contains B.h,b.h.

The following procedure is wrong

2. Using the # include directive may result in a single header file being included multiple times, reducing compilation efficiency

For example, the following conditions:

A one function is declared in One.h, and One.h is included in the Two.h, by the way, a single function is declared. (This does not write the implementation of the function, that is, the definition of the function)

If I want to use one and two two functions in main.c, and sometimes we don't necessarily know that two.h contains one.h, we might do this:

The code for MAIN.C after the pre-processing of the compilation is this:

void One (); void One (); void both (); int Main () {    return0;}

Line 1th is caused by the # include "One.h", and the 2nd, 3 lines are caused by the # include "Two.h" (because Two.h contains one.h). As you can see, the one function is declared 2 times and is not necessary at all, which reduces compilation efficiency.

In order to solve this problem of repeating the same header file, we usually write the contents of the header file:

To explain the meaning, take one.h for example: When we first include "one.h", because there is no definition of _one_h_, so the condition of the 9th line is established, and then the 10th line defines the _ONE_H_ this macro, and then declare the one function in line 13, Finally, the conditional compilation ends in line 15. When the second # include "One.h", because previously defined _ONE_H_ this macro, so the 9th line of the condition is not set, jump directly to the 15th line of #endif, end conditional compilation. This is the simple 3-sentence code that prevents one.h content from being repeatedly included.

In this case, the MAIN.C:

#include "one.h" #include "two.h"

It becomes:

 //  #include "one.h"   #ifndef _one_h_   #define  _one_h_void   one ();   #endif  //  #include "two.h"   #ifndef _two_h_   #define  _two_h_//  #include "one.h"   #ifndef _one_h_   #define  _one_h_void   one ();   #endif  void  ";   #endif  

The 7th line of section 2~ is caused by the # include "One.h", and the 23rd line of section 10~ is caused by the # include "Two.h". After the preprocessing of the compilation, it becomes:

1 void One (); 2 void ();

That's the result we want.

17-c language preprocessing Directive 3-file contains

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.