0Pricing
C Academy · Lesson

Include Guards

Prevent double inclusion.

Include Guards is a free C Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Double-Include Problem

Headers often include other headers. It is easy for one header to end up included twice in the same source file.

When that happens, every declaration in it appears twice. For type definitions like struct or typedef, that is a compile error: a type cannot be defined more than once.

How It Sneaks In

Suppose a.h and b.h both include types.h. A source file that includes both ends up with types.h pasted in twice.

The preprocessor blindly copies header text, so types.h's definitions duplicate, triggering a redefinition error.

/* a.h */ #include "types.h"
/* b.h */ #include "types.h"
/* main.c */
#include "a.h"
#include "b.h"   /* types.h now seen twice */

The Include Guard

An include guard is a small preprocessor trick that makes a header safe to include many times.

It wraps the whole header in a conditional so the body is processed only the first time. Later inclusions are skipped entirely.

#ifndef TYPES_H
#define TYPES_H

/* header contents here */

#endif /* TYPES_H */

How the Guard Works

The first time the header is seen, TYPES_H is not defined, so #ifndef is true and the body is processed. The #define then sets the macro.

On any later inclusion, TYPES_H already exists, so #ifndef is false and the preprocessor skips to #endif.

#ifndef TYPES_H   /* true only the first time */
#define TYPES_H   /* remember we've been here */
/* ... body ... */
#endif

A Complete Guarded Header

Here is a realistic header with a guard protecting a type definition.

Even if many files include point.h directly or indirectly, the Point struct is defined exactly once per translation unit.

/* point.h */
#ifndef POINT_H
#define POINT_H

typedef struct {
    int x;
    int y;
} Point;

Point point_add(Point a, Point b);

#endif

Choosing the Macro Name

The guard macro must be unique across your whole project, or two headers could clash and silently hide each other.

A common convention derives the name from the filename in upper case: point.h becomes POINT_H, net/socket.h becomes NET_SOCKET_H.

#ifndef NET_SOCKET_H
#define NET_SOCKET_H
/* ... */
#endif

Avoid Reserved Names

Identifiers that begin with an underscore followed by a capital letter, or that contain double underscores, are reserved for the compiler and standard library.

Avoid guards like __POINT_H__. A plain trailing form such as POINT_H is safe and clear.

#ifndef _POINT_H_   /* risky: reserved name pattern */
#ifndef POINT_H     /* preferred */

The #pragma once Alternative

Most modern compilers support #pragma once, a single line at the top of a header that does the same job.

It is shorter and avoids naming clashes, but it is not part of the C standard. Traditional #ifndef guards work everywhere.

/* point.h */
#pragma once

typedef struct { int x; int y; } Point;

Guards vs Pragma

#ifndef guards are fully portable and standard, but require a unique macro per header.

#pragma once is compact and clash-free, but relies on compiler support. Some projects use both: a pragma plus a classic guard for maximum safety.

#pragma once
#ifndef POINT_H
#define POINT_H
/* ... */
#endif

Guards Are Per Translation Unit

An include guard prevents duplicate inclusion within a single source file as it is compiled.

It does not prevent a header from being processed once in each separate .c file. That is fine, because each source file is a distinct translation unit.

Make It a Habit

Add an include guard to every header you write, from the very first line. It costs three lines and prevents a whole class of confusing errors.

Even simple headers can later be included indirectly through others, so guarding from the start saves trouble.

/* config.h */
#ifndef CONFIG_H
#define CONFIG_H
#define MAX_USERS 100
#endif

Quick Check

Check your grasp of include guards.

Recap

You learned why including a header twice causes redefinition errors and how an include guard prevents it.

Use #ifndef/#define/#endif with a unique macro, or #pragma once. Next we will look at sharing variables across files with extern.

Frequently asked questions

Is the “Include Guards” lesson free?

Yes — the full text of “Include Guards” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “Include Guards”?

Prevent double inclusion. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Include Guards” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Header and Source Files
  2. Include Guards
  3. extern and Linkage
  4. Compiling Multiple Files
← Back to C Academy