Loading lessons...
Multi-File Programs
Multi-File Programs
Big programs split source into .c files and declarations into headers (.h).
Header file: mymath.h
C
#ifndef MYMATH_H
#define MYMATH_H
int square(int x);
#endifMain program
C
#include "mymath.h"
int main() {
printf("%d", square(5));
return 0;
}Separate .c for the implementation
C
int square(int x) { return x * x; }Compile together
Bash
gcc main.c mymath.c -o appInclude guard
#ifndef MYMATH_H ... #endif prevents double inclusion.
TL;DR
- Headers hold declarations.
- .c files hold definitions.
- Include guards stop duplication.
- List all .c files when compiling.