C language file inclusion Processing

Source: Internet
Author: User

1.
Function: one source file can contain all the content of another source file.

2.
General format:
# Include"
File Name"


Or # include
<
File Name>

Differences:


When angle brackets are used, the compiler starts from C
Find the files to be included in the directory where the standard library files are located, which is called the standard method;


When you use the double marker, the system first searches for the files to be included in the user's current directory. If the files cannot be found, the system then searches for the files in the standard way.

3.
Included File Content

Source file (*. c)

Header file (*. h)

4.
Description

1
) One # include
Command can only specify one file to be included, if you want to include N
Files to use N
# Include
Command;

2
) If file 1
Include File 2
And file 2
File 3
In file1.c
Definition:

# Include
"File3.h"

# Include
"File2.h"

3
) File inclusion can be nested;

4
) The file to which the file is contained has become the same file after pre-compilation. Therefore, if
There is a global variable, which is also in file2.c
The file is valid and does not need to use extern.
Statement.

Note:

A header file may be contained multiple times in a source file with a condition indicator to prevent repeated processing of such header files.

For example:

# Ifndef
Bookstore_h

# Define
Bookstore_h

# Endif

Condition Indicator # ifndef
Check bookstore_h
Whether it has been defined previously. Here bookstore_h
It is a precompiler constant. Traditionally, precompiler constants are often written in uppercase letters, such as bookstore_h.
If it is not defined before, the value of the Condition Indicator is true, so from # ifndef
To # endif
All statements are included for processing. Conversely, if # ifndef
If the value of the indicator is false, it matches with # endif
Lines between indicators are ignored. To ensure that the header file is only processed once, set the following # define
Indicator

# Define
Bookstore_h

Put in # ifndef
In this way, when the header file content is processed for the first time, bookstore_h
Will be defined to prevent the # ifndef
The value of the indicator is true.

 

This policy works well as long as there are no two header files that must be included to check a Preprocessor constant with the same name. # Ifdef
Indicators are often used to determine whether a Preprocessor constant has been defined to include program code with conditions.


# Ifdef
In addition to preventing repeated inclusion, it can also be used for Conditional compilation for different environments. Frequently seen are:

# If defined (_ GCC __)
Zookeeper
Defined (_ sun_c __)
  

Such commands are pre-processing commands for selecting and Compiling different compilers and platforms. When the compiling environment does not define this specific symbol, the pre-processor will automatically ignore this part of the code. Besides
# Ifdef
Determine whether a symbol is defined. You can also determine the value of the symbol. VC
Common
Msc_ver
Compare with a specific value to determine the VC
This is the practice of version.



In addition to the content mentioned above, there are some relatively rare pre-processor commands, such
# Pragma
. They provide some special optimization methods and compiler control methods, which can significantly improve the code quality and reduce the coding burden. However, in many cases, using these compilation commands may be incompatible with other compilation environments, therefore, when selecting non-standard preprocessing commands in the project, we should make a decision based on the portability and encoding advantages.

 

File Inclusion refers to the inclusion of another C language source program in a C language source program through the include pre-processing command.


General format:

# Include "included file name"

Or # include <included File Name>

Purpose: Include the specified file in the current file and insert it to the corresponding location of the file containing command. The use of file inclusion commands can reduce repetitive work by program designers and improve program development efficiency.

Note:

(1) The contained files are generally designated as header files (*. H), and can also be files such as C Programs.

(2) One include command can only specify one contained file. If you want to include N files, you need to use N include commands.

(3) cannot contain OBJ files. File Inclusion is processed before compilation, not during connection.

(4) When the file name is enclosed in double quotation marks, the system first searches for the contained file in the current directory. If the file name cannot be found, it searches for other directories in the standard method specified by the system. When angle brackets are used, the system directly searches based on the specified standard method.


The header file provided by the system is enclosed in angle brackets. A custom file with double quotation marks.

(5) The contained file and the current file are converted into the same file after pre-compilation, rather than the two files.

(6) file inclusion can be nested, but must be included in order.

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.