Programming in C: Unit I (d): Preprocessor Directives

#include

with Example C Programs | Preprocessor Directives

An external file containing function, variables or macro definitions can be included as a part of our program. This avoids the effort to re-write the code that is already written.

#INCLUDE

An external file containing function, variables or macro definitions can be included as a part of our program. This avoids the effort to re-write the code that is already written. The #include directive is used to inform the preprocessor to treat the contents of a specified file as if those contents had already appeared in the source program at the point where the directive appears.

The #include directive can be used in two forms. Both the forms make the preprocessor insert the entire contents of the specified file into the source code of our program. However, the difference between the two is the way in which they search for the specified.

#include <filename>

This variant is used for system header files. When we include a file using angular brackets, a search is made for the file named filename in a in a standard list of system directories.

#include "filename"

This variant is used for header files of your own program. When we include a file using double quotes, the preprocessor searches the file named filename first in the directory containing the current file, then in the quote directories and then the same directories used for

<filename>

Note

The filename can optionally be preceded by a directory specification. For example, you may specify the exact path by writing "c:\students\my_header.h".

Points to Remember

The preprocessor stops searching the directory as soon as it finds a file with the given name.

If a completely unambiguous path for the file is specified between double quotation marks (" "), then the preprocessor searches only that path specification and ignores the standard directories.

If an incomplete path is specified for the filename in double quotes, then the preprocessor first searches the parent file's directory (where a parent file is the one which contains the #include directive. For example, if you include a file named file2 within a file named file1,file1 is the parent file).

File inclusion can be 'nested'; i.e., a #include directive can appear in a file named by another #include directive. For example, filel can include file2, and in turn file2 can include another file named file3. In this case, filel would be the parent of file2 and the grandparent of file3.

When file inclusion is nested and when compiling is done from the command line, directory searching begins with the directories of the parent file and then proceeds through the directories of any grandparent files.

Note

Nesting of include files can continue up to 10 levels.

Programming in C: Unit I (d): Preprocessor Directives : Tag: : with Example C Programs | Preprocessor Directives - #include