What is the usage of include in C language?

烟雨青岚
Release: 2020-06-15 15:45:55
Original
17219 people have browsed it

What is the usage of include in C language?

#What is the usage of include in c language?

1. Introduction to #include command

#include command is a kind of preprocessing command. The preprocessing command can convert other source codes into Content is inserted into the specified location; it can identify a certain piece of program code that will only be compiled under specific conditions;

can define macros with similar identifier functions. During compilation, the preprocessor will use Other text replaces the macro.

2. Insert the contents of the header file

##include command tells the preprocessor to insert the contents of the specified header file into the corresponding location of the preprocessor command . There are two ways to specify the header file to be inserted:

#include <文件名>
#include "文件名"
Copy after login

If you need to include the standard library header file or the header file provided by the implementation version, the first format should be used. As shown in the following example:

#include <math.h>               // 一些数学函数的原型,以及相关的类型和宏
Copy after login

If you need to include source files developed for the program, you should use the second format. Files inserted using the #include command usually have a .h file extension. The files include function prototypes, macro definitions and type definitions.

These definitions can be used by any source file as long as the #include command is used. As shown in the following example:

#include "myproject.h" // Function prototypes, type definitions and macros used in the current project

You can use macros in the #include command. If a macro is used, the macro's substitution must ensure that the correct #include command is generated.

Example 1 shows such an #include command.

[Example 1] Macros in the #include command

#ifdef        _DEBUG_
  #define       MY_HEADER       "myProject_dbg.h"
#else
  #define       MY_HEADER       "myProject.h"
#endif
#include        MY_HEADER
Copy after login

When the above program code enters preprocessing, if the DEBUG macro has been defined, then the preprocessor The contents of myProject_dbg.h will be inserted; if it has not been defined, the contents of myProject.h will be inserted.

3. How the preprocessor finds the header file

The search path for the file specified by the #include command is determined by the given C language implementation version. At the same time, it is also up to the implementation version to determine whether the file name is case-sensitive. For files specified using angle brackets () in the command, the preprocessor usually searches in specific system paths. For example, on Unix systems, the paths /usr/local/include and /usr/include are searched.

For files specified in double quotes ("filename") in the command, the preprocessor usually first looks in the current directory, which is the directory containing other source files for the program. If not found in the current directory, the preprocessor will also search the system's include path. The file name can contain a path. But if the file name contains a path, the preprocessor will only look in that directory.

You can also specify your own search path for the #include command by using compiler command line options or by adding the search path to an environment variable (usually called INCLUDE). For specific methods, please refer to the documentation of the compiler used.

4. Nested #include commands

#include commands can be used nested; that is, the source file itself inserted through the #include command You can also include another #include command. The preprocessor allows up to 15 levels of nested includes.

Because a header file sometimes contains another header file, it is easy for the same file to be included multiple times. For example, suppose the file myProject.h contains the following code:

#include <stdio.h>
Copy after login

If the source file contains the following #include command, stdio.h will be included twice, once directly and once indirectly:

#include <stdio.h>
#include "myProject.h"
Copy after login

However, you can use conditional compilation commands to conveniently avoid including the same file multiple times. Example 2 uses this technique.

[Example 2] Avoid multiple inclusions

#ifndef INCFILE_H_
#define INCFILE_H_
/* ...实际的头文件incfile.h的内容写在这里... */
#endif  /* INCFILE_H_ */
Copy after login

The first time a command containing incfile.h appears, the INCFILE_H_ macro is not defined. The preprocessor therefore inserts the content between #ifndef and #endif, which contains the definition of the INCFILE_H_ macro. After embedding the incfile.h file, the #ifndef condition will be false and the preprocessor will ignore the content between #ifndef and #endif.

Recommended tutorial: "C Language"

The above is the detailed content of What is the usage of include in C language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template