Course contentsShow
← C
Lesson 800 of 1,21911. The Preprocessor and BuildFree lesson

Organizing /src and /include folders

Organizing /src and /include folders

What you'll learn: How to separate your source code from your header files to keep growing projects clean and professional.

As your C programs grow from a single file into a collection of multiple .c and .h files, your project folder can quickly become a cluttered "junk drawer." To stay organized, professional C developers use a standardized folder structure: they put logic in a /src folder and definitions in an /include folder.

The Kitchen Cabinet Analogy

Think of your project like a professional kitchen.

The /src (source) folder is the stove area. This is where the heavy lifting happens. It contains your .c files—the actual "recipes" and active cooking logic.

The /include folder is the spice rack. It contains your .h header files. These aren't the meal itself; they are the labels and ingredients that the cook needs to reference to make sure everything works together. By keeping the spices (headers) on a separate shelf from the stove (logic), you prevent the workspace from becoming chaotic.

A Typical Structure

In a multi-file project, your file tree should look like this:

my_project/
├── include/
│   └── physics.h
├── src/
│   ├── main.c
│   └── physics.c
└── Makefile

How to Link Them

When you move your headers into a separate /include folder, you have to tell the compiler where to find them.

Inside your .c files, you still use the #include directive. However, since the header is no longer in the same folder as the source code, you typically tell your compiler (like GCC) where the "spice rack" is located using the -I (include) flag.

Here is how the top of src/main.c would look:

/* src/main.c */
#include <stdio.h>
#include "physics.h" // The compiler looks in /include because we tell it to

int main() {
    float force = calculate_gravity(10.0, 5.0);
    printf("Force: %.2f\n", force);
    return 0;
}

When you compile, you would run a command like gcc src/main.c src/physics.c -Iinclude -o my_app. The -Iinclude part tells the preprocessor: "If you can't find a header file in the current folder, check the /include directory next."

This separation ensures that anyone reading your code can immediately see the "interface" (the headers) in one place and the "implementation" (the source) in another.

Key takeaway

Store .c implementation files in /src and .h header files in /include to keep your project scalable and organized.