C Reference ยท Preprocessor
The #include directive
Reference for the C #include directive: angle brackets versus quotes, search paths, include guards, macro-expanded includes and common mistakes.
#include tells the preprocessor to replace the directive with the entire contents of another file. It is how a source file gets declarations for functions, types and macros that live elsewhere: the standard library headers, your own headers, or third-party libraries. The compiler never sees the #include line, only the pasted text.
There are two forms. Angle brackets (<stdio.h>) search the implementation's standard and system directories. Double quotes ("util.h") search first in an implementation-defined place, usually the directory of the current file, and then fall back to the same places as the angle form.
#include <stdio.h> /* standard or system header */ #include "util.h" /* your own header */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
#include <h-char-sequence> #include "q-char-sequence" #include pp-tokens /* macro-expanded to one of the forms above */
| Form | Description |
|---|---|
<name> |
Searches the implementation's include directories (system and -I paths). |
"name" |
Searches the including file's directory first (typically), then the <name> places. |
| macro form | Tokens after #include are macro-expanded; the result must match one of the two forms. |
Errors: if the file cannot be found, compilation stops with a fatal error. Including a file that includes itself without a guard recurses until the compiler's nesting limit.
Usage notes
- Use quotes for your project's headers, angle brackets for system ones. That convention tells readers where a header comes from.
- Add include guards (or
#pragma once) to every header so including it twice is harmless. See Include guards. - Include what you use. Do not rely on one header pulling in another; the standard does not guarantee it, and it breaks when a library is updated.
- Header contents: declarations,
typedefs, macros,static inlinefunctions. Not variable definitions or ordinary function definitions, or every file that includes it creates a duplicate symbol. - Search paths are added with compiler flags (
-I dir). The path inside the quotes may contain directories ("net/socket.h"), but separators other than/are not portable. - Computed includes work:
#define CONFIG "config_linux.h"then#include CONFIG. Use sparingly. - Test availability with
__has_include(standard since C23, a long-standing GCC and Clang extension). - Headers are not libraries.
#include <math.h>declaressqrt, but some toolchains also need-lmat link time.