C Header Guards

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Header Guards?

A header guard wraps a header in #ifndef NAME / #define NAME / #endif so its contents are processed only once per translation unit. Without it, including a header twice (directly or through other headers) redefines types and triggers errors. #pragma once is a widely supported shortcut.

Remember
#ifndef GEOMETRY_H
#define GEOMETRY_H
/* declarations, types, macros */
#endif /* GEOMETRY_H */

#pragma once      /* non-standard but common */

Cheat Sheet

Keep these facts handy while you work with Header Guards in C.

TopicDetail
NameUnique, usually FILE_H
Pattern#ifndef + #define + #endif
Alternative#pragma once
Needed forStruct definitions, typedefs

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Predefined Macros.

Example 1 — Including a guarded header twice

The second include expands to nothing, so the struct is defined once.

C
/* point.h */
#ifndef POINT_H
#define POINT_H
struct Point { int x, y; };
#endif

/* main.c */
#include <stdio.h>
#include "point.h"
#include "point.h"   /* skipped by the guard */

int main(void) {
    struct Point p = {3, 4};
    printf("p = (%d, %d)\n", p.x, p.y);
    return 0;
}

How It Works

  1. The first include defines POINT_H and the struct.
  2. The second include sees POINT_H is defined and skips the contents.
  3. Without the guard, struct Point would be redefined and the compiler would report an error.

Edge Cases & Pitfalls

Check these before relying on Header Guards in real programs.

dup

Duplicate guard names

Two headers using the same guard macro hide each other. Include the project/path in the name.

rsvd

Reserved identifiers

Names that begin with an underscore followed by a capital, or contain __, are reserved.

Key Takeaways

  • Always guard headers.
  • Unique names.
  • #pragma once is shorter but non-standard.

One line: A header guard wraps a header in #ifndef NAME / #define NAME / #endif so its contents are processed only once per translation unit.

Frequently Asked Questions

A header may be included several times through different paths. Guards stop duplicate definitions, which would otherwise cause compile errors.
Almost every major compiler supports it, but it is not part of the C standard. Traditional guards work everywhere.
Continue with "C Predefined Macros" next. Use __LINE__, __FILE__, __DATE__, __STDC__ and friends. The C Preprocessor hub lists every topic in order.

Next: Predefined Macros

Use __LINE__, __FILE__, __DATE__, __STDC__ and friends.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful