Learn C
#include "functions/header_files.h"

Headers & Multi-File Programs in C

#concept

Headers (.h) publish what a .c file provides: function prototypes, struct layouts, macros, and extern declarations — everything another file needs to call in. Sources (.c) define the bodies. This split lets ten files share one interface while compiling independently, joined at the end by the linker.

Hygiene rules keep it working: every header gets include guards, headers include what they use (self-contained), never define functions or non-extern variables in headers (multiple-definition errors), and keep private helpers static in their .c file.

#how it works
1

Headers declare, sources define

math.h: int square(int); — math.c: the body. Callers include the header and never see the implementation.

2

Guard every header

#ifndef MATH_H / #define MATH_H ... #endif (or #pragma once) makes double-inclusion impossible.

3

Never define in headers

Function bodies and plain globals in headers multiply-define across includers — declare there, define in exactly one .c.

4

Compile together, link once

gcc main.c math.c -o app — each compiles alone against declarations, the linker resolves everything after.

#example
// math.h:
// #ifndef MATH_H
// #define MATH_H
// int square(int x);
// #endif
// ---- app built as: gcc main.c math.c -o app
#include <stdio.h>
#include "math.h"
int main(void) {
printf("%d\n", square(6));
return 0;
}
Output
36
1main.c sees only square's prototype via the guarded header — enough to compile the call.
2math.c compiled separately provides the real body in its own object file.
3The linker joins both objects; 36 prints through the header-declared interface.
#use cases
Splitting growing programs into interface (.h) plus implementation (.c)
Publishing library APIs consumers include without sources
Guarding every header so inclusion order never matters
Keeping private helpers static and header-free

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.