Module 8 · Files, the Preprocessor and Program Organisation — topic 3 of 4
Header Files and Multi-File Programs
Real programs are split into several .c files with shared declarations in .h files. Learn what belongs in a header, include guards, separate compilation and linking, and writing a small Makefile.
Learning objectives
- Split a program into an interface (.h) and implementation (.c)
- Prevent double inclusion with include guards
- Compile and link multiple files with gcc and make
Definition
A header file (.h) contains declarations — function prototypes, type definitions, macros andexternvariable declarations — that are shared between translation units by#include. Definitions live in exactly one source file (.c).
Introduction
When a program grows to thousands of lines, one file becomes unmanageable. C programs are organised as modules: stack.c implements a stack and stack.h declares what other files may use. main.c includes stack.h and calls those functions.
Each .c file is compiled separately into an object file; the linker joins them. Change one file and only that file needs recompiling — which is what tools like make automate.
Why it matters
Every intermediate and advanced project in this course uses multiple source files and a Makefile, exactly as industry codebases do.
Linker errors (“multiple definition of”, “undefined reference to”) make sense once you know what belongs in headers versus sources.
Detailed explanation
Put in headers: function prototypes, struct/enum/typedef definitions, macros, and extern declarations of shared variables. Put in sources: function bodies and the single definition of each global variable.
Include guards prevent a header's contents being processed twice when it is included through several paths: #ifndef STACK_H / #define STACK_H / … / #endif.
A source file should include its own header first; this checks that the header is self-contained and that the definitions match the declarations.
Build: gcc -c stack.c → stack.o; gcc -c main.c → main.o; gcc main.o stack.o -o app links them. Or all at once: gcc main.c stack.c -o app.
Mark helpers used only inside one .c file static so they don't appear in the global namespace (internal linkage).
Syntax
- Use quotes for your own headers and angle brackets for system headers.
- The guard name is usually the file name in capitals.
- Build with
gcc main.c counter.c -o app.
Code examples
Build commands
Output: 5. Only the changed file needs recompiling before linking again.
A minimal Makefile
Recipe lines must start with a tab. make rebuilds only what changed; make clean removes build output.
More examples
Interface and implementation in one view
300 200 100
The three files are shown concatenated so the example can run as one program; in a project each section is its own file and the commented #include "stack.h" lines are real.
Step-by-step explanation
stack.hdeclares theStacktype and three functions: this is the module's public interface.- The include guard makes it safe for several files to include
stack.h. stack.cimplements the functions;main.cuses them without knowing how they work.stack_popreports success through its return value and delivers the value through a pointer.- Pushing 100, 200, 300 and popping returns them in reverse order (last in, first out).
Expected output
Running “Interface and implementation in one view” prints exactly:
Practical applications
- Libraries ship headers plus compiled
.a/.so/.dllfiles; you include the header and link the library. - Large projects (Linux, Git) have hundreds of
.c/.hpairs built by make or CMake. - Teams agree on header interfaces so modules can be developed in parallel.
Best practices
- One header per module; include it first in its own
.c. - Keep headers minimal: only what other files need.
- Automate builds with a Makefile or CMake from the start of a project.
Common mistakes
Defining functions or variables in a header
Avoid
Prefer
If two .c files include the header, the linker sees two definitions and fails with “multiple definition”.
Forgetting to compile a source file
Avoid
Prefer
The linker cannot find stack_push's definition: “undefined reference to stack_push”.
Important notes
static inlinefunctions may be defined in headers because each translation unit gets its own private copy.- Header names are case-sensitive on Linux;
#include "Stack.h"fails if the file isstack.h.
Key takeaways
- Headers declare; source files define.
- Include guards prevent double inclusion.
- Compile each
.cfile and link all object files together.
Try it yourself
- 1Split the calculator from the switch lesson into
calc.h,calc.candmain.c, and write a Makefile. - 2Create a header with a struct definition and include it twice in one file — first without guards, then with guards — and compare the errors.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
3 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is the purpose of `#ifndef X_H / #define X_H / ... / #endif` in a header?
- ModerateTwo .c files include a header containing `int counter = 0;`. What happens at link time?
- AdvancedWhy can a `static inline` function be defined in a header included by many .c files without linker errors?