Go to the first, previous, next, last section, table of contents.

How `#include' Works

The `#include' directive works by directing the C preprocessor to scan the specified file as input before continuing with the rest of the current file. The output from the preprocessor contains the output already generated, followed by the output resulting from the included file, followed by the output that comes from the text after the `#include' directive. For example, given a header file `header.h' as follows,

char *test ();

and a main program called `program.c' that uses the header file, like this,

int x;
#include "header.h"

main ()
{
  printf (test ());
}

the output generated by the C preprocessor for `program.c' as input would be

int x;
char *test ();

main ()
{
  printf (test ());
}

Included files are not limited to declarations and macro definitions; those are merely the typical uses. Any fragment of a C program can be included from another file. The include file could even contain the beginning of a statement that is concluded in the containing file, or the end of a statement that was started in the including file. However, a comment or a string or character constant may not start in the included file and finish in the including file. An unterminated comment, string constant or character constant in an included file is considered to end (with an error message) at the end of the file.

It is possible for a header file to begin or end a syntactic unit such as a function definition, but that would be very confusing, so don't do it.

The line following the `#include' directive is always treated as a separate line by the C preprocessor even if the included file lacks a final newline.


Go to the first, previous, next, last section, table of contents.