Skip to content
Guest
Learn C · Course Syllabus Show lessons
Modular C · 8 min read · Intermediate

Headers, Include Guards & Preprocessor

Split C programs across .h headers and .c implementation files cleanly using #ifndef include guards and safe macros.

What you'll learn

  • Separate public declarations (.h) from private implementations (.c)
  • Prevent duplicate definition errors with #ifndef / #define / #endif include guards
  • Avoid macro side-effect pitfalls by preferring static inline functions

Before the C compiler parses your code, the preprocessor handles directives starting with # (#include, #define, #ifdef). Putting declarations in .h headers with include guards prevents multiple-inclusion errors across multi-file projects.

Fill-in-the-Blank Exercise

Header Include Guard

Guard MATH_UTILS_H so the header is processed at most once per translation unit.

 MATH_UTILS_H
 MATH_UTILS_H
int clamp(int v, int lo, int hi);
#endif

Live Code Lab

Cpreprocessor.c
Expected OutputClick “Run Live” to compile with GCC 13 (C11)
Clamped: 100 (max=100)

Check Your Understanding

Score: 0 / 1

1. Why are static inline functions generally safer than function-like #define macros in C?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind