The #include directive

A preprocessor include directive causes the preprocessor to replace the directive with the contents of the specified file.

Read syntax diagramSkip visual syntax diagram#include directive syntax
 
>>-#--include--+-"--file_name--"---+---------------------------><
               +-<--file_name-->---+
               +-<--header_name-->-+
               '-identifiers-------'
 

In all C and C++ implementations, the preprocessor resolves macros contained in an #include directive. After macro replacement, the resulting token sequence must consist of a file name enclosed in either double quotation marks or the characters < and >.

For example:

#define MONTH <july.h>
#include MONTH

If the file name is enclosed in double quotation marks, for example:

#include "payroll.h"

the preprocessor treats it as a user-defined file, and searches for the file in a manner defined by the preprocessor.

If the file name is enclosed in angle brackets, for example:

#include <stdio.h>

it is treated as a system-defined file, and the preprocessor searches for the file in a manner defined by the preprocessor.

The new-line and > characters cannot appear in a file name delimited by < and >. The new-line and " (double quotation marks) character cannot appear in a file name delimited by " and ", although > can.

Declarations that are used by several files can be placed in one file and included with #include in each file that uses them. For example, the following file defs.h contains several definitions and an inclusion of an additional file of declarations:

/* defs.h */
#define TRUE 1
#define FALSE 0
#define BUFFERSIZE 512
#define MAX_ROW 66
#define MAX_COLUMN 80
int hour;
int min;
int sec;
#include "mydefs.h"

You can embed the definitions that appear in defs.h with the following directive:

#include "defs.h"

In the following example, a #define combines several preprocessor macros to define a macro that represents the name of the C standard I/O header file. A #include makes the header file available to the program.

#define C_IO_HEADER <stdio.h>

/* The following is equivalent to:
 *   #include <stdio.h>
 */

#include C_IO_HEADER