#include

The file inclusion directive is the most used. It is very rare to write a 'C' source file without using it. It has two possible forms:

	#include "filespec.h"
or
	#include <filespec.h>

In both cases, the action of the #include preprocessor directive is to take the contents of the file named by 'filespec' and to insert the contents of the named file, replacing the directive, at the point where the directive appears in the 'C' source or input file. The only difference between the two is where the compiler will look for the named file.

The #include directive can be nested. That is, the included file can contain any preprocessor directives and operators, including the #include directive. Most compilers have a practical limit to the nesting level of #include's. This is typically on the order of 10 for most compilers.

In the first instance, where the filespec is in double quote marks, e.g.:

	#include "data.h"

the compiler will look for the named file according to the following rules. If the named 'filespec' has an absolute directory path, then it will look only there for the file. If the path is incomplete or relative, then the compiler will first look relative to the directory of the original file containing the directive where the compilation is taking place. If the file is not found then the "standard" places will be searched for the file to be included. Any nested relative #include directives will be relative to the original file.

The "standard" places where the compiler will search are specified to the compiler in two ways. The first is via the command line, and the second is compiler dependent - but include:

	Environment variables (of the operating system),
	Special configuration files installed for the compiler,
	Compiled into the compiler itself.

The list of places to search given on the command line is always examined before looking in other standard places.

In the second instance, where the filespec is in angle brackets, e.g.:

	#include <stdio.h>

the compiler will look for the named file according to the following rules. First the "standard" places, as mentioned above are searched. If a file with the given name is not found, then the compiler will look relative to the directory of the original file containing the directive where the compilation is taking place - as in the previous case.


© 1991-2008 Prem Sobel. All Rights Reserved.