File Inclusion
File inclusion makes it easy to handle collections of #defines and declarations (among other things). Any source line of the form:
#include "filename"
or:
#include <filename>
is replaced by the contents of the file filename. If the filename is quoted, searching for the file typically begins where the source program was found; if it is not found there, or if the name is enclosed in <
and >
, searching follows an implementation-defined rule to find the file. An included file may itself contain #include
lines.
There are often several #include
lines at the beginning of a source file, to include common #define
statements and extern declarations, or to access the function prototype declarations for library functions from headers like <stdio.h>
. (Strictly speaking, these need not be files; the details of how headers are accessed are implementation-dependent.)
#include
is the preferred way to tie the declarations together for a large program. It guarantees that all the source files will be supplied with the same definitions and variable declarations, and thus eliminates a particularly nasty kind of bug. Naturally, when an included file is changed, all files that depend on it must be recompiled.