As programs grow, you split them into files. Header files hold the shared promises.
Direct answer
A header file (.h) declares functions, structs and constants so that several .c files can use them. The source file (.c) contains the definitions. Add include guards to avoid duplicate definitions.
Example
math_utils.h
#ifndef MATH_UTILS_H
#define MATH_UTILS_H
int add(int a, int b);
#endif
math_utils.c
#include "math_utils.h"
int add(int a, int b) {
return a + b;
}
main.c
#include <stdio.h>
#include "math_utils.h"
int main(void) {
printf("%d\n", add(2, 3));
return 0;
}
Compile:
gcc main.c math_utils.c -o app
Tips
- Use
<angle brackets>for system headers and"quotes"for your own. - Do not define variables in headers.
Where Teyro fits
Teyro's C for Beginners course turns this into short lessons you can play in a few minutes a day. It has 78 lessons, and you can start free.
The bottom line
Headers declare, source files define.



