Source file inclusion
Includes another source file into the current source file at the line immediately after the directive.
Syntax
#include < h-char-sequence > new-line
|
(1) | ||||||||
#include " q-char-sequence " new-line
|
(2) | ||||||||
#include pp-tokens new-line
|
(3) | ||||||||
__has_include ( " q-char-sequence " ) __has_include ( < h-char-sequence > )
|
(4) | (since C23) | |||||||
__has_include ( string-literal ) __has_include ( < h-pp-tokens > )
|
(5) | (since C23) | |||||||
new-line | - | The new-line character |
h-char-sequence | - | A sequence of one or more h-chars, where the appearance of any of the following causes undefined behavior:
|
h-char | - | Any member of the source character set except new-line and > |
q-char-sequence | - | A sequence of one or more q-chars, where the appearance of any of the following causes undefined behavior:
|
q-char | - | Any member of the source character set except new-line and " |
pp-tokens | - | A sequence of one or more preprocessing tokens |
string-literal | - | A string literal |
h-pp-tokens | - | A sequence of one or more preprocessing tokens except > |
Explanation
include
in the directive are processed just as in normal text (i.e., each identifier currently defined as a macro name is replaced by its replacement list of preprocessing tokens). The directive resulting after all replacements shall match one of the two previous forms. The method by which a sequence of preprocessing tokens between < and > preprocessing token pair or a pair of " characters is combined into a single header name preprocessing token is implementation-defined.__has_include
expression evaluates to 1 if the search for the source file succeeds, and to 0 if the search fails.In the case the file is not found, the program is ill-formed.
|
(since C23) |
Notes
Typical implementations search only standard include directories for syntax (1). The standard C library is implicitly included in these standard include directories. The standard include directories usually can be controlled by the user through compiler options.
The intent of syntax (2) is to search for the files that are not controlled by the implementation. Typical implementations first search the directory where the current file resides then falls back to (1).
When a file is included, it is processed by translation phases 1-4, which may include, recursively, expansion of the nested #include
directives, up to an implementation-defined nesting limit. To avoid repeated inclusion of the same file and endless recursion when a file includes itself, perhaps transitively, header guards are commonly used: the entire header is wrapped in
#ifndef FOO_H_INCLUDED /* any name uniquely mapped to file name */ #define FOO_H_INCLUDED // contents of the file are here #endif
Many compilers also implement the non-standard pragma #pragma once with similar effects: it disables processing of a file if the same file (where file identity is determined in OS-specific way) has already been included.
A __has_include
result of 1 only means that a header or source file with the specified name exists. It does not mean that the header or source file, when included, would not cause an error or would contain anything useful.
Example
This section is incomplete Reason: no example |
References
- C23 standard (ISO/IEC 9899:2023):
- 6.4.7 Header names (p: 69)
- 6.10.1 Conditional inclusion (p: 165-169)
- 6.10.2 Source file inclusion (p: 169-170)
- C17 standard (ISO/IEC 9899:2018):
- 6.10.2 Source file inclusion (p: 119-120)
- C11 standard (ISO/IEC 9899:2011):
- 6.10.2 Source file inclusion (p: 164-166)
- C99 standard (ISO/IEC 9899:1999):
- 6.10.2 Source file inclusion (p: 149-151)
- C89/C90 standard (ISO/IEC 9899:1990):
- 3.8.2 Source file inclusion
See also
A list of C Standard Library header files |