|
1 | 1 | ---
|
2 | 2 | description: "Learn more about: #include directive (C/C++)"
|
3 | 3 | title: "#include directive (C/C++)"
|
4 |
| -ms.date: "08/29/2019" |
| 4 | +ms.date: 01/19/2021 |
5 | 5 | f1_keywords: ["#include"]
|
6 | 6 | helpviewer_keywords: ["preprocessor, directives", "#include directive", "include directive (#include)"]
|
7 |
| -ms.assetid: 17067dc0-8db1-4f2d-b43e-ec12ecf83238 |
8 | 7 | ---
|
9 |
| -# #include directive (C/C++) |
| 8 | +# `#include` directive (C/C++) |
10 | 9 |
|
11 |
| -Tells the preprocessor to treat the contents of a specified file as if they appear in the source program at the point where the directive appears. |
| 10 | +Tells the preprocessor to include the contents of a specified file at the point where the directive appears. |
12 | 11 |
|
13 | 12 | ## Syntax
|
14 | 13 |
|
15 |
| -> **#include** "*path-spec*"\ |
16 |
| -> **#include** \<*path-spec*> |
| 14 | +> **`#include "`** *path-spec* **`"`**\ |
| 15 | +> **`#include <`** *path-spec* **`>`** |
17 | 16 |
|
18 | 17 | ## Remarks
|
19 | 18 |
|
20 |
| -You can organize constant and macro definitions into include files and then use **#include** directives to add them to any source file. Include files are also useful for incorporating declarations of external variables and complex data types. The types may be defined and named only once in an include file created for that purpose. |
| 19 | +You can organize constant and macro definitions into *include files* (also known as *header files*) and then use **`#include`** directives to add them to any source file. Include files are also useful for incorporating declarations of external variables and complex data types. The types may be defined and named only once in an include file created for that purpose. |
21 | 20 |
|
22 | 21 | The *path-spec* is a file name that may optionally be preceded by a directory specification. The file name must name an existing file. The syntax of the *path-spec* depends on the operating system on which the program is compiled.
|
23 | 22 |
|
24 |
| -For information about how to reference assemblies in a C++ application that's compiled by using [/clr](../build/reference/clr-common-language-runtime-compilation.md), see [#using](../preprocessor/hash-using-directive-cpp.md). |
| 23 | +For information about how to reference assemblies in a C++ application that's compiled by using [`/clr`](../build/reference/clr-common-language-runtime-compilation.md), see [`#using` directive](../preprocessor/hash-using-directive-cpp.md). |
25 | 24 |
|
26 |
| -Both syntax forms cause that directive to be replaced by the entire contents of the specified include file. The difference between the two forms is the order in which the preprocessor searches for header files when the path is incompletely specified. The following table shows the difference between the two syntax forms. |
| 25 | +Both syntax forms cause the **`#include`** directive to be replaced by the entire contents of the specified file. The difference between the two forms is the order of the paths that the preprocessor searches when the path is incompletely specified. The following table shows the difference between the two syntax forms. |
27 | 26 |
|
28 |
| -|Syntax Form|Action| |
29 |
| -|---|------------| |
30 |
| -|Quoted form|The preprocessor searches for include files in this order:<br/><br/> 1) In the same directory as the file that contains the **#include** statement.<br/><br/> 2) In the directories of the currently opened include files, in the reverse order in which they were opened. The search begins in the directory of the parent include file and continues upward through the directories of any grandparent include files.<br/><br/> 3) Along the path that's specified by each **/I** compiler option.<br/><br/> 4) Along the paths that are specified by the INCLUDE environment variable.| |
31 |
| -|Angle-bracket form|The preprocessor searches for include files in this order:<br/><br/> 1) Along the path that's specified by each **/I** compiler option.<br/><br/> 2) When compiling occurs on the command line, along the paths that are specified by the INCLUDE environment variable.| |
| 27 | +| Syntax Form | Action | |
| 28 | +|--|--| |
| 29 | +| Quoted form | The preprocessor searches for include files in this order:<br/><br/> 1) In the same directory as the file that contains the **`#include`** statement.<br/><br/> 2) In the directories of the currently opened include files, in the reverse order in which they were opened. The search begins in the directory of the parent include file and continues upward through the directories of any grandparent include files.<br/><br/> 3) Along the path that's specified by each **`/I`** compiler option.<br/><br/> 4) Along the paths that are specified by the `INCLUDE` environment variable. | |
| 30 | +| Angle-bracket form | The preprocessor searches for include files in this order:<br/><br/> 1) Along the path that's specified by each **`/I`** compiler option.<br/><br/> 2) When compiling occurs on the command line, along the paths that are specified by the `INCLUDE` environment variable. | |
32 | 31 |
|
33 | 32 | The preprocessor stops searching as soon as it finds a file that has the given name. If you enclose a complete, unambiguous path specification for the include file between double quotation marks (`" "`), the preprocessor searches only that path specification and ignores the standard directories.
|
34 | 33 |
|
35 |
| -If the file name that's enclosed in double quotation marks is an incomplete path specification, the preprocessor first searches the "parent" file's directory. A parent file is the file that contains the **#include** directive. For example, if you include a file named *file2* in a file named *file1*, *file1* is the parent file. |
| 34 | +If the file name that's enclosed in double quotation marks is an incomplete path specification, the preprocessor first searches the *parent* file's directory. A parent file is the file that contains the **`#include`** directive. For example, if you include a file named *file2* in a file named *file1*, *file1* is the parent file. |
36 | 35 |
|
37 |
| -Include files can be "nested": An **#include** directive can appear in a file that's named by another **#include** directive. For example, *file2* could include *file3*. In this case, *file1* would still be the parent of *file2*, but it would be the "grandparent" of *file3*. |
| 36 | +Include files can be *nested*: An **`#include`** directive can appear in a file that's named by another **`#include`** directive. For example, *file2* could include *file3*. In this case, *file1* would still be the parent of *file2*, but it would be the grandparent of *file3*. |
38 | 37 |
|
39 |
| -When include files are nested and when compiling occurs on the command line, directory searching begins in the directories of the parent file. Then it proceeds through the directories of any grandparent files. That is, searching begins relative to the directory that contains the source that's currently being processed. If the file isn't found, the search moves to directories that are specified by the [/I (Additional include directories)](../build/reference/i-additional-include-directories.md) compiler option. Finally, the directories that are specified by the INCLUDE environment variable are searched. |
| 38 | +When include files are nested and when compiling occurs on the command line, directory searching begins in the directory of the parent file. Then it proceeds through the directories of any grandparent files. That is, searching begins relative to the directory that contains the source that's currently being processed. If the file isn't found, the search moves to directories that are specified by the [`/I` (Additional include directories)](../build/reference/i-additional-include-directories.md) compiler option. Finally, the directories that are specified by the `INCLUDE` environment variable are searched. |
40 | 39 |
|
41 |
| -From the Visual Studio development environment, the INCLUDE environment variable is ignored. For information about how to set the directories that are searched for include files and library files, see [VC++ Directories Property Page](../build/reference/vcpp-directories-property-page.md). |
| 40 | +Within the Visual Studio development environment, the `INCLUDE` environment variable is ignored. The values specified in the project properties for include directories are used instead. For more information about how to set the include directories in Visual Studio, see [Include Directories](../build/reference/vcpp-directories-property-page.md#directory-types) and [Additional Include Directories](../build/reference/c-cpp-prop-page.md#additional-include-directories). |
42 | 41 |
|
43 | 42 | This example shows file inclusion by using angle brackets:
|
44 | 43 |
|
45 | 44 | ```C
|
46 | 45 | #include <stdio.h>
|
47 | 46 | ```
|
48 | 47 |
|
49 |
| -This example adds the contents of the file named STDIO.H to the source program. The angle brackets cause the preprocessor to search the directories that are specified by the INCLUDE environment variable for STDIO.H, after it searches directories that are specified by the **/I** compiler option. |
| 48 | +The example adds the contents of the file named *`stdio.h`* to the source program. The angle brackets cause the preprocessor to search the directories that are specified by the `INCLUDE` environment variable for *`stdio.h`*, after it searches directories that are specified by the **`/I`** compiler option. |
50 | 49 |
|
51 | 50 | The next example shows file inclusion by using the quoted form:
|
52 | 51 |
|
53 | 52 | ```C
|
54 | 53 | #include "defs.h"
|
55 | 54 | ```
|
56 | 55 |
|
57 |
| -This example adds the contents of the file that's specified by DEFS.H to the source program. The quotation marks mean that the preprocessor first searches the directory that contains the parent source file. |
| 56 | +The example adds the contents of the file that's specified by *`defs.h`* to the source program. The quotation marks mean that the preprocessor first searches the directory that contains the parent source file. |
58 | 57 |
|
59 |
| -Nesting of include files can continue up to 10 levels. When the nested **#include** is processed, the preprocessor continues to insert the enclosing include file into the original source file. |
| 58 | +Nesting of include files can continue up to 10 levels. When processing of the nested **`#include`** is finished, the preprocessor continues to insert the enclosing parent include file into the original source file. |
60 | 59 |
|
61 |
| -**Microsoft Specific** |
| 60 | +**Microsoft-specific** |
62 | 61 |
|
63 |
| -To locate includable source files, the preprocessor first searches the directories that are specified by the **/I** compiler option. If the **/I** option isn't present, or if it fails, the preprocessor uses the INCLUDE environment variable to find any include files within angle brackets. The INCLUDE environment variable and **/I** compiler option can contain multiple paths, separated by semicolons (**;**). If more than one directory appears as part of the **/I** option or within the INCLUDE environment variable, the preprocessor searches them in the order in which they appear. |
| 62 | +To locate the source files to include, the preprocessor first searches the directories specified by the **`/I`** compiler option. If the **`/I`** option isn't present, or if it fails, the preprocessor uses the `INCLUDE` environment variable to find any include files within angle brackets. The `INCLUDE` environment variable and **`/I`** compiler option can contain multiple paths, separated by semicolons (**`;`**). If more than one directory appears as part of the **`/I`** option or within the `INCLUDE` environment variable, the preprocessor searches them in the order in which they appear. |
64 | 63 |
|
65 | 64 | For example, the command
|
66 | 65 |
|
67 | 66 | ```cmd
|
68 |
| -CL /ID:\MSVC\INCLUDE MYPROG.C |
| 67 | +CL /ID:\msvc\include myprog.c |
69 | 68 | ```
|
70 | 69 |
|
71 |
| -causes the preprocessor to search the directory D:\MSVC\INCLUDE\ for include files such as STDIO.H. The commands |
| 70 | +causes the preprocessor to search the directory *`D:\msvc\include\`* for include files such as *`stdio.h`*. The commands |
72 | 71 |
|
73 | 72 | ```cmd
|
74 |
| -SET INCLUDE=D:\MSVC\INCLUDE |
75 |
| -CL MYPROG.C |
| 73 | +SET INCLUDE=D:\msvc\include |
| 74 | +CL myprog.c |
76 | 75 | ```
|
77 | 76 |
|
78 | 77 | have the same effect. If both sets of searches fail, a fatal compiler error is generated.
|
79 | 78 |
|
80 |
| -If the file name is fully specified for an include file that has a path that includes a colon (for example, F:\MSVC\SPECIAL\INCL\TEST.H), the preprocessor follows the path. |
| 79 | +If the file name is fully specified for an include file that has a path that includes a colon (for example, *`F:\MSVC\SPECIAL\INCL\TEST.H`*), the preprocessor follows the path. |
81 | 80 |
|
82 |
| -For include files that are specified as `#include "path-spec"`, directory searching begins with the directory of the parent file and then proceeds through the directories of any grandparent files. That is, searching begins relative to the directory that contains the source file that contains the **#include** directive that's being processed. If there is no grandparent file and the file has not been found, the search continues as if the file name were enclosed in angle brackets. |
| 81 | +For include files that are specified as `#include "path-spec"`, directory search begins in the directory of the parent file and then proceeds through the directories of any grandparent files. That is, the search begins relative to the directory that contains the source file that's being processed. If there's no grandparent file and the file still isn't found, the search continues as if the file name were enclosed in angle brackets. |
83 | 82 |
|
84 |
| -**END Microsoft Specific** |
| 83 | +**END Microsoft-specific** |
85 | 84 |
|
86 | 85 | ## See also
|
87 | 86 |
|
88 | 87 | [Preprocessor directives](../preprocessor/preprocessor-directives.md)\
|
89 |
| -[/I (Additional include directories)](../build/reference/i-additional-include-directories.md) |
| 88 | +[`/I` (Additional include directories)](../build/reference/i-additional-include-directories.md) |
0 commit comments