0Pricing
C Academy · Lesson

Header and Source Files

Separate declarations and code.

Header and Source Files is a free C Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Split Code Into Files?

As C programs grow, keeping everything in one .c file becomes hard to read and slow to compile.

C lets you split a project into multiple files: each handles one concern. This improves organization, lets you reuse code, and means changing one part doesn't force you to recompile everything.

Two Kinds of Files

A modular C project uses two file types.

Source files (.c) contain the actual implementation: function bodies and definitions.

Header files (.h) contain declarations: function prototypes, type definitions, and macros that other files need to know about.

A Header File

A header declares what exists without saying how it works.

Here math_utils.h declares an add function. Any file that includes this header learns the function's signature and can call it.

/* math_utils.h */
int add(int a, int b);

The Matching Source File

The source file provides the implementation. It includes its own header so the compiler can check the definition matches the declaration.

Notice the header is included with quotes, used for your own project files.

/* math_utils.c */
#include "math_utils.h"

int add(int a, int b) {
    return a + b;
}

Using the Module

Now main.c includes the header and calls add. It never sees the function body, only the prototype.

The compiler trusts the declaration; the linker later connects the call to the real implementation in math_utils.c.

/* main.c */
#include <stdio.h>
#include "math_utils.h"

int main(void) {
    printf("%d\n", add(2, 3));
    return 0;
}

Quotes vs Angle Brackets

The #include directive has two forms.

#include <stdio.h> uses angle brackets for system and standard library headers. The compiler searches its standard include paths.

#include "math_utils.h" uses quotes for your own headers, searched first in the current directory.

#include <stdio.h>      /* standard library */
#include "math_utils.h" /* your project */

What Belongs in a Header

Put declarations in headers, not definitions.

Good header contents: function prototypes, struct and typedef declarations, #define macros, and extern variable declarations.

Avoid putting function bodies or variable definitions there, as that can cause duplicate-symbol errors.

/* shapes.h */
typedef struct {
    double width;
    double height;
} Rect;

double rect_area(Rect r);

What Belongs in a Source File

The source file holds the real work: function definitions and the file's private helpers.

It includes the matching header so the compiler verifies signatures agree. This catches mismatches early instead of at link time.

/* shapes.c */
#include "shapes.h"

double rect_area(Rect r) {
    return r.width * r.height;
}

Declaration vs Definition

This distinction is central to modular C.

A declaration tells the compiler a name's type and signature so it can be used: int add(int, int);

A definition creates the actual entity, allocating storage or providing the body. Headers declare; source files define.

int add(int a, int b);          /* declaration */
int add(int a, int b) {         /* definition */
    return a + b;
}

Naming Conventions

A common pattern pairs each module by name: logger.h with logger.c, parser.h with parser.c.

This makes it obvious where an implementation lives. Group related functions into one module so each file has a single clear responsibility.

/* logger.h */
void log_info(const char *msg);
void log_error(const char *msg);

The Build Picture

Each .c file is compiled independently into an object file, then all objects are linked into one program.

Headers are not compiled on their own; they are textually inserted into the source files that include them. We will explore the full build flow in a later lesson.

gcc -c main.c        /* -> main.o */
gcc -c math_utils.c  /* -> math_utils.o */
gcc main.o math_utils.o -o app

Quick Check

Test your understanding of header and source roles.

Recap

You learned to split a C project into .h headers and .c source files.

Headers declare what exists; source files define how it works. Use quotes for your own headers and angle brackets for standard ones.

Next we will prevent problems caused by including a header more than once.

Frequently asked questions

Is the “Header and Source Files” lesson free?

Yes — the full text of “Header and Source Files” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “Header and Source Files”?

Separate declarations and code. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Header and Source Files” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Header and Source Files
  2. Include Guards
  3. extern and Linkage
  4. Compiling Multiple Files
← Back to C Academy