[C Language] 17-preprocessing command 3-File Inclusion

Source: Internet
Author: User

Note:This C language topic is a prelude to learning iOS development. In order to allow users with experience in object-oriented language developmentProgramWho can quickly get started with the C language. If you have no programming experience or are not interested in C and iOS development, ignore

This section describes the last preprocessing command-File Inclusion

I. Basic Concepts

As a matter of fact, we already have access to the file to include this command, that is, # include, which can copy all the content of one file to another.

 

II. General Form 1. 1st forms # Include <File Name>

Go to the directory where the function header file of the C language library is located to find the file.

 

2. 2nd forms# Include "file name"

The system firstSearch in the current directory in sequence. If not, search in the path of the operating system, and then find in the directory where the function header file of the C language library is located.

 

Iii. Usage notes

1. # include commandAllowNested include, for example, A. H contains B. H, B. H contains C. H,Not AllowedRecursive inclusion. For example, A. H contains B. H, and B. H contains a. h.

The following method is as follows:ErrorOf


 

2. Using the # include command may cause multiple accesses to the same head file, reducing compilation efficiency.

For example:


In one. H, a one function is declared; in two. H, one. H is included, and a two function is declared by the way. (The function implementation is not written here, that is, the function definition)

Assume that I want to use one and two functions in Main. C, andSometimes we do not necessarily know that two. h contains one. H, soIt may do this:

Compile prefixAfter processing, the main. cCodeYes:

  1   void   one ();   2   void   one ();   3   void   two ();   4   int   main ()   5  {  6   7   return   0  ;  8 }

1st rows are caused by # include "One. H", and 2nd and 3 rows are caused by # include "Two. H ".Of(Because two. h contains one. h). You can seeIf the one function is declared twice, it is unnecessary. This will reduce the compilation efficiency.

To solve this problem of repeatedly containing the same header file, we usually write the header file content as follows:


Take one for a general explanation. h: for the first time # include "one. H ", because _ one_h _ is not defined, the condition of row 9th is true, and then defined in row 10th._ One_h _ macro, declare the one function in 13 rows, and end the condition compilation in 15 rows. When the second time# Include "One. H"Because we have already defined_ One_h _This macro, so the condition of the 9th rows is not true, Jump directly to the # endif of the 15th rows, and end the condition compilation. It is such a simple three-sentence code that prevents the content of one. h from being repeatedly included.

In this case, in Main. C:

 
# Include"One. h"# Include"Two. h"

It becomes:

 1   //  # Include "One. H"  2   #Ifndef_ One_h _  3   # Define _ One_h _ 4  5   Void  One ();  6   7   # Endif  8   9   //  # Include "Two. H"  10   #Ifndef_ Two_h _  11   # Define _ Two_h _ 12  13   //  # Include "One. H"  14   #Ifndef_ One_h _  15   # Define _ One_h _ 16   17   Void  One ();  18   19   # Endif  20  21   Void  Two ();  22   23   # Endif 

2nd ~ The first line is # include "One. H ".,10th ~ The first line is caused by # include "Two. H. After compilation and preprocessing, it becomes:

 
1 VoidOne ();2 VoidTwo ();

This is what we want.

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.