C in 100 Seconds: Multi-File Projects
Video: C in 100 Seconds: Multi-File Projects — Headers, Linking, Make | Episode 41 by Taught by Celeste AI - AI Coding Coach
Watch full page →Multi-File Projects
Real C projects split code across multiple files. A header file declares functions, an implementation file defines them, and main uses them.
The Header File — math_utils.h
#ifndef MATH_UTILS_H
#define MATH_UTILS_H
int add(int a, int b);
int multiply(int a, int b);
#endif
The #ifndef / #define / #endif pattern is an include guard — it prevents the header from being included twice. The declarations tell the compiler what functions exist without providing the code.
The Implementation — math_utils.c
#include "math_utils.h"
int add(int a, int b) {
return a + b;
}
int multiply(int a, int b) {
return a * b;
}
Quotes in #include "math_utils.h" mean look in the current directory. Angle brackets mean look in the system path.
Using It — main.c
#include <stdio.h>
#include "math_utils.h"
int main() {
int sum = add(10, 20);
int product = multiply(5, 6);
printf("add(10, 20) = %d\n", sum);
printf("multiply(5, 6) = %d\n", product);
return 0;
}
Compiling
Pass all .c files to gcc: gcc main.c math_utils.c -o app. Or use a Makefile to automate it — Make only recompiles files that changed.
Student Code
Try it yourself: episode41/