Header Files
Part of the Object Oriented Programming section of Coddy's C journey — lesson 1 of 61.
As C programs grow larger, keeping all code in a single file becomes unmanageable. Header files provide a way to organize your code by separating what a function does from how it does it.
A header file (with the .h extension) contains function declarations (also called prototypes). These declarations tell the compiler that a function exists and what parameters it takes, without providing the actual implementation.
// math_utils.h
int add(int a, int b);
To use declarations from a header file, you include it with the #include directive. For your own headers, use double quotes instead of angle brackets:
// main.c
#include "math_utils.h"
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3);
return 0;
}
The key difference: #include <stdio.h> searches system directories, while #include "math_utils.h" first searches your project folder. This separation allows multiple files to share the same function declarations, making your codebase easier to navigate and maintain.
Cheat sheet
Header files (with .h extension) contain function declarations that tell the compiler a function exists without providing its implementation:
// math_utils.h
int add(int a, int b);
Include your own header files using double quotes:
// main.c
#include "math_utils.h"
int add(int a, int b) {
return a + b;
}
#include <stdio.h> searches system directories, while #include "math_utils.h" searches your project folder first.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Modular Programming Basics
Header FilesInclude GuardsSource FilesStatic FunctionsRecap: Modular Calculator4Encapsulation
Opaque Pointers ConceptDefining Opaque StructsGetters and SettersValidation in SettersRecap: Secret Box2Objects and Methods
Structs as ObjectsThe 'Self' PointerConst CorrectnessPointer vs ValueHelper MethodsRecap: Point Manager5Project: Simple Bank Account
Project SetupImplementation of Account3Object Lifecycle
Constructor PatternDestructor PatternStack InitializationDeep CopyRecap: String Wrapper6Inheritance via Composition
Struct EmbeddingThe First Member RuleAccessing Parent MembersUpcastingRecap: Shape Hierarchy