Back to Blog

C in 100 Seconds: Header Files | Episode 16

Daryl WongDaryl Wong

Video: C in 100 Seconds: Three Files One Program — Header Files | Episode 16 by Taught by Celeste AI - AI Coding Coach

Watch full page →

Header Files — Splitting Code Across Files in C

C in 100 Seconds, Episode 16


Real C projects don't live in a single file. Header files let you declare functions in one place and use them everywhere.

The Problem

If you define a function in one .c file and try to call it from another, the compiler won't know it exists. You need a way to share function declarations across files.

The Header File

math_utils.h

#ifndef MATH_UTILS_H
#define MATH_UTILS_H

int square(int x);
int cube(int x);

#endif

The header contains declarations — just the function signatures, no bodies. The #ifndef/#define/#endif pattern is an include guard that prevents the file from being included twice.

The Implementation

math_utils.c

#include "math_utils.h"

int square(int x) {
  return x * x;
}

int cube(int x) {
  return x * x * x;
}

The .c file includes its own header and provides the actual function bodies.

Using It

main.c

#include <stdio.h>
#include "math_utils.h"

int main() {
  printf("square(5) = %d\n", square(5));
  printf("cube(3) = %d\n", cube(3));
  return 0;
}

Angle brackets (<stdio.h>) search the system include path. Double quotes ("math_utils.h") search the current directory first.

Include Guards

Without the #ifndef guard, including the same header twice would cause duplicate definition errors. The guard ensures the contents are only processed once per compilation unit.

Why Does This Matter?

Header files are how C scales. Every library you use — stdio.h, string.h, stdlib.h — is a header file declaring functions whose implementations live elsewhere. Understanding this pattern is essential for writing modular C code.

Compile and Run

gcc main.c math_utils.c -o program
./program

Note: you compile both .c files together. The compiler links them into one binary.


Next episode: Pointers — understanding addresses and the heart of C.

Student code: github.com/GoCelesteAI/c-in-100-seconds/episode16