Include Guards
An include guard is a pair of preprocessor directives that make a header's contents expand only the first time it is included, so including it twice in one translation unit is harmless.
Detailed Explanation
#include is textual: the preprocessor replaces the directive with the entire content of the named file. Nothing stops it from doing that twice. In a real project it will, because includes nest. Suppose shape.h and canvas.h both include types.h, and main.c includes both. types.h arrives twice.
For plain function declarations that is merely wasteful, since repeating a declaration is legal. For anything that is a definition it is an error. A struct definition, a typedef, or an enum may appear only once in a scope. The second copy produces redefinition of 'struct Point', and the message points at the header while the cause is the include graph. In C11 a repeated typedef naming the same type became legal, but struct and enum definitions still are not, so you cannot rely on that.
An include guard turns the header into a one-shot. It uses a macro as a flag. The header opens with #ifndef POINT_H ("if this macro is not defined yet") followed immediately by #define POINT_H, and closes with #endif on the last line. The first inclusion finds the macro undefined, defines it, and emits the body. Every later inclusion in that same translation unit finds it defined and the preprocessor deletes everything down to the #endif. Note the scope: guards are per translation unit, because macro definitions do not survive past the end of one. Each .c file starts over with a clean slate, which is exactly what you want.
Naming matters more than it looks. The guard macro shares one flat namespace with every other macro in the whole translation unit, so a collision silently blanks out one of the two headers. Use something derived from the project and path, such as MYLIB_NET_SOCKET_H, not UTILS_H. Avoid names beginning with an underscore followed by a capital letter, or containing a double underscore, because the standard reserves those for the implementation.
#pragma once does the same job in one line. Put it at the top of the header and the compiler skips the file if it has already processed it in this translation unit. Every mainstream compiler supports it, and it is faster in principle because the compiler can decide not to reopen the file rather than reading it and throwing it away. It is not in any C standard, and its behavior depends on how the compiler decides whether two paths are "the same file".
That last point is where it bites. #pragma once compares file identity, not content. If a header is reachable through a symlink and through its real path, or through two different include directories, or it has been copied into two places in a vendored tree, the compiler may treat the copies as different files and include both. Include guards compare a macro name, so two byte-identical copies of a header still guard each other correctly. Conversely, if two unrelated headers accidentally use the same guard macro, #pragma once would have worked and the guard silently drops one.
Practical advice: use a properly namespaced #ifndef guard, which is portable and standard. Adding #pragma once above it costs nothing and can speed up large builds. Whatever you pick, be consistent, and never ship a header with no protection at all.
Code Examples
/* point.h — the classic guard. */
#ifndef MYAPP_POINT_H /* project-qualified, so it will not collide */
#define MYAPP_POINT_H
struct Point { /* a definition: may appear only once per TU */
double x;
double y;
};
double point_distance(struct Point a, struct Point b);
#endif /* MYAPP_POINT_H *//* main.c — proves the guard works. Both headers include point.h. */
#include <math.h>
#include <stdio.h>
#include "point.h" /* first copy: contents expand */
#include "point.h" /* second copy: preprocessor deletes it all */
double point_distance(struct Point a, struct Point b)
{
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}
int main(void)
{
struct Point origin = {0.0, 0.0};
struct Point target = {3.0, 4.0};
printf("%.1f\n", point_distance(origin, target)); /* prints: 5.0 */
return 0;
}
/* Build with: cc main.c -o main -lm
Remove the #ifndef/#define/#endif from point.h and the same file fails:
redefinition of 'struct Point' */Common Gotchas
A guard macro reused by two headers silently discards one of them
the second header sees the flag already set and expands to nothing, so you get `unknown type name` errors far from the real cause. Derive the name from the full path, and never use bare names like `UTILS_H` or `COMMON_H`.
`#pragma once` keys on file identity, not contents
symlinks, two include paths reaching the same header, or a vendored duplicate can each defeat it and include the file twice. This is the one concrete reason to keep the `#ifndef` form as your default.
Anything before the `#ifndef` is not guarded
a stray `#include` or declaration above the guard line gets pulled in on every inclusion. The guard must be the first thing in the file apart from comments, and the matching `#endif` the last.
Guards do not protect across translation units
they stop double inclusion inside one `.c` file only. Preventing duplicate *symbols* across files is a different problem, solved with `extern` declarations and a single definition, not with guards.