Skip to content

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.

Beginner 15 min read·Lesson 33 of 37 in C Programming

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 and extern variable 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

C
/* counter.h */
#ifndef COUNTER_H
#define COUNTER_H
void counter_add(int n);
int counter_value(void);
#endif
/* counter.c */
#include "counter.h"
static int total = 0; /* private to counter.c */
void counter_add(int n) { total += n; }
int counter_value(void) { return total; }
/* main.c */
#include <stdio.h>
#include "counter.h"
int main(void) { counter_add(5); printf("%d\n", counter_value()); }
  • 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

Terminal
gcc -std=c11 -Wall -c counter.c # -> counter.o
gcc -std=c11 -Wall -c main.c # -> main.o
gcc counter.o main.o -o app # link
./app

Output: 5. Only the changed file needs recompiling before linking again.

A minimal Makefile

Makefile
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra
app: main.o counter.o
$(CC) $(CFLAGS) -o app main.o counter.o
main.o: main.c counter.h
counter.o: counter.c counter.h
clean:
rm -f app *.o

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

C
/* ---- stack.h ---- */
#ifndef STACK_H
#define STACK_H
#include <stdbool.h>
#define STACK_CAP 8
typedef struct { int items[STACK_CAP]; int top; } Stack;
void stack_init(Stack *s);
bool stack_push(Stack *s, int v);
bool stack_pop(Stack *s, int *out);
#endif
/* ---- stack.c ---- */
/* #include "stack.h" */
void stack_init(Stack *s) { s->top = 0; }
bool stack_push(Stack *s, int v) {
if (s->top == STACK_CAP) return false;
s->items[s->top++] = v;
return true;
}
bool stack_pop(Stack *s, int *out) {
if (s->top == 0) return false;
*out = s->items[--s->top];
return true;
}
/* ---- main.c ---- */
#include <stdio.h>
/* #include "stack.h" */
int main(void) {
Stack s;
stack_init(&s);
for (int i = 1; i <= 3; i++) stack_push(&s, i * 100);
int v;
while (stack_pop(&s, &v)) printf("%d ", v);
printf("\n");
return 0;
}
Output
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.h declares the Stack type and three functions: this is the module's public interface.
  • The include guard makes it safe for several files to include stack.h.
  • stack.c implements the functions; main.c uses them without knowing how they work.
  • stack_pop reports 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:

TextOutput
300 200 100

Practical applications

  • Libraries ship headers plus compiled .a/.so/.dll files; you include the header and link the library.
  • Large projects (Linux, Git) have hundreds of .c/.h pairs 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

/* util.h */
int count = 0;
int twice(int x) { return 2 * x; }

Prefer

/* util.h */
extern int count;
int twice(int x);
/* util.c */
int count = 0;
int twice(int x) { return 2 * x; }

If two .c files include the header, the linker sees two definitions and fails with “multiple definition”.

Forgetting to compile a source file

Avoid

gcc main.c -o app # stack.c missing

Prefer

gcc main.c stack.c -o app

The linker cannot find stack_push's definition: “undefined reference to stack_push”.

Important notes

  • static inline functions 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 is stack.h.

Key takeaways

  • Headers declare; source files define.
  • Include guards prevent double inclusion.
  • Compile each .c file and link all object files together.

Try it yourself

  1. 1
    Split the calculator from the switch lesson into calc.h, calc.c and main.c, and write a Makefile.
  2. 2
    Create 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?
Practise this topic