Names specified here
Name |
Description |
Notes
|
Source |
Availability |
#include |
Include header |
L |
|
|
|
Directive |
C89
|
C90
|
C95
|
C99
|
C11
|
Suppose you need a type available in two separate translation
units:
struct point {
int x, y;
};
Copying such a declaration manually into the two
corresponding source files is not only tedious, but also
error-prone, and doubly so if you decide later to alter
it.
Instead, shared declarations should be placed in
headers, usually text files with a suffix
.h. For example:
//
struct point {
int x, y;
};
This declaration is now available in any source file where
it is needed, simply by using the directive #include
:
#include "point.h"
double distance(const struct point *p)
{
return sqrt(p->x * p->x + p->y * p->y);
}
The technique works equally well with function
prototypes, in-line function declarations
and macro definitions that
need to be shared across source files. Related types,
functions and macros are usually grouped together in a single
header, rather than defining a header for each one. Extending
our example, we could declare distance
together with the type it operates on:
//
struct point {
int x, y;
};
double distance(const struct point *p);
A header can also be included using this form:
#include <something.h>
This usually tells the preprocessor to look for the header
in a preconfigured location, such as where external libraries are
stored, rather than within the same directory as the source
files. C provides a Standard Library, which is exposed
through its Standard headers,
which should always be included using the form <>
.
An #include
directive matches the grammar of control-line:
-
control-line
# include pp-tokens
new-line
- header-name
< h-char-sequence >
" q-char-sequence "
- h-char-sequence
h-char
h-char-sequence h-char
- q-char-sequence
q-char
q-char-sequence q-char
- h-char
-
- q-char
-
Its pp-tokens are subject to macro expansion, and the result
must match header-name.
Header contents
The following may be placed in headers:
Of course, they don't have to be placed in headers,
unless you need to use them in more than one source
file.
The following should not normally be placed in header
files:
Protection against multiple
inclusions
Headers also get preprocessed, so they are subject to macro expansion, and can
contain further directives, including #include
.
This can lead to a problem of including a header twice by
mistake. Suppose foo.h includes
bar.h and baz.h, and both of
these include qux.h. Any source file
including foo.h will inadvertantly include
the contents of qux.h twice. Repeated
function prototypes in qux.h won't cause a
problem, but redefinitions of macros will, as will
redefinitions of types, even if the definitions are
identical. To avoid this, the header contents should be
protected by choosing an unused macro name, defining it
when the header is included, and using #ifndef
to detect when it has
already been defined, and therefore whether the header
has already been included:
//
#ifndef POINT_HEADER_INCLUDED
#define POINT_HEADER_INCLUDED
struct point {
int x, y;
};
double distance(const struct point *p);
#endif