Include Guards in C
Headers get included multiple times through diamond include chains (a.h includes b.h and c.h, both including common.h) — without guards, common.h's contents paste twice and redefinition errors follow. Include guards (#ifndef FILE_H / #define FILE_H ... #endif, or #pragma once) make repetition harmless.
Every header you write needs one, named after its path (MATH_UTILS_H). #pragma once is shorter and universally supported in practice, though not technically standard — classic guards work everywhere including ancient toolchains.
Diamond includes duplicate
Two paths to one header paste its contents twice — structs and typedefs then redefine and fail.
Classic guard pattern
#ifndef MATH_H / #define MATH_H ...contents... #endif — second inclusion finds MATH_H defined and contributes nothing.
Name guards uniquely
PROJECT_MODULE_H mirrors the file path — generic names like COMMON_H collide across projects.
#pragma once alternative
One line, same effect, supported by GCC/Clang/MSVC — choose it for new code unless exotic compilers matter.
// math.h:// #ifndef MATH_H// #define MATH_H// int square(int x);// #endif// main.c includes math.h twice (directly + via util.h):// -> second paste contributes nothing; compiles cleanlyRecommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.