Separating interface from implementation
What you'll learn: How to split your code into .h and .c files to keep your projects organized and professional.
Imagine you are ordering a pizza. You don't need to know the exact temperature of the oven or the secret brand of flour the chef uses. You only need the menu—a list of what you can order and what you get back. In C, we use Header Files as the "menu" and Source Files as the "kitchen."
The Menu: The Header File (.h)
The header file is the interface. It tells other parts of your program what functions exist without showing how they work. It contains the function prototypes. If you share your code with someone else, they only need to read this file to understand how to use your tools.
The Kitchen: The Source File (.c)
The source file is the implementation. This is where the actual logic lives. By keeping the "how-to" code separate from the "what-it-does" list, you make your project much easier to maintain. If you want to change how a calculation works later, you only change the .c file; the rest of your program (which only cares about the interface) doesn't have to change at all.
How it looks in code
Suppose we are building a simple math module. We split it into two files:
/* math_utils.h - The Interface */
// This tells the world: "I have a function that adds two integers."
int add(int a, int b);
/* math_utils.c - The Implementation */
#include "math_utils.h"
// This is the actual work being done.
int add(int a, int b) {
return a + b;
}
To use this in your main program, you simply #include "math_utils.h". This allows your main.c to call the add() function while staying blissfully ignorant of the underlying code.
Why bother?
As your programs grow from 50 lines to 5,000, putting everything in one file becomes a nightmare. Modularization allows you to organize code by theme (like "graphics," "input," or "physics"). It speeds up compilation and makes it possible for teams to work on different parts of the same project simultaneously without stepping on each other's toes.
Key takeaway
Use .h files to announce what your functions do and .c files to define how they do it.