Header Files
A header file holds the declarations that describe a module's interface, so other source files can use the module without seeing how it is implemented. #include copies the header's text into the file that includes it.
Detailed Explanation
The compiler sees one translation unit at a time, so every name a file uses must be declared in that file. Without headers, each .c file would have to repeat the declarations of everything it calls, by hand, and any typo would go unnoticed until the program misbehaved at run time. A header is one shared copy of those declarations that both the implementer and every caller include, so all of them are checked against the same text.
The split hinges on the difference between a declaration and a definition. A declaration introduces a name and its type: int stack_push(Stack *s, int value);. A definition creates the thing: the function body, or the storage for a variable. Declarations may be repeated as often as you like, as long as they agree. Definitions of an external name must occur exactly once in the whole program.
That single rule decides what goes where.
Belongs in a .h:
- function declarations (prototypes)
externdeclarations of global variablesstruct,unionandenumdefinitions, andtypedefs that callers need- macro definitions that are part of the interface
static inlinefunction definitions, which are per-translation-unit and therefore safe (C99)
Must not go in a .h:
- a normal function definition with a body, unless it is
staticorstatic inline; every including TU would define the symbol and the link fails withmultiple definition - a global variable definition such as
int verbose_level;for the same reason; writeextern int verbose_level;in the header and put the definition in one.cfile
A good header is small and self-sufficient. Self-sufficient means it compiles on its own: if a declaration uses size_t, the header includes <stddef.h> itself rather than hoping the caller included it first. Small means you expose the least you can. If callers only ever hold a pointer to your struct, declare typedef struct Stack Stack; and keep the fields in the .c file. The incomplete type stops callers from poking at internals and means changing the fields does not force them to recompile.
There are two include forms. #include <stdio.h> searches the implementation's system directories, plus anything you add with -I. #include "stack.h" searches "in a manner associated with the source file" first, which on every mainstream compiler means the directory of the including file, and then falls back to the same places as the angle-bracket form. The standard leaves the details implementation-defined, so the practical convention is: angle brackets for the standard library and for third-party dependencies, quotes for headers belonging to your own project.
The cost of headers is build time and coupling. Everything a header includes lands in every TU that includes it, and editing a widely included header recompiles the world. Guard every header against double inclusion, and prefer forward declarations over includes when a pointer is all you need.
Code Examples
/* stack.h — the interface. Declarations only. */
#ifndef STACK_H
#define STACK_H
#include <stddef.h> /* this header uses size_t, so it includes it itself */
/* Incomplete type: callers can hold a Stack *, but cannot see the fields. */
typedef struct Stack Stack;
Stack *stack_create(size_t capacity); /* NULL on allocation failure */
void stack_destroy(Stack *s); /* safe to call with NULL */
int stack_push(Stack *s, int value); /* 0 on success, -1 if full */
extern const char *stack_version; /* declared here, defined in stack.c */
#endif /* STACK_H *//* stack.c — the implementation. Definitions live here. */
#include <stdlib.h>
#include "stack.h" /* include your own header first in the .c file:
it proves the header is self-sufficient */
const char *stack_version = "1.0"; /* the one definition in the program */
struct Stack { /* the complete type, invisible to callers */
size_t capacity;
size_t count;
int *items;
};
Stack *stack_create(size_t capacity)
{
Stack *s = malloc(sizeof *s); /* sizeof *s: no type to keep in sync */
if (s == NULL) return NULL; /* malloc can fail; handle it */
s->items = malloc(capacity * sizeof *s->items);
if (s->items == NULL) {
free(s); /* undo the first allocation */
return NULL;
}
s->capacity = capacity;
s->count = 0;
return s;
}
void stack_destroy(Stack *s)
{
if (s == NULL) return; /* free(NULL) is fine, but s->items would not be */
free(s->items);
free(s);
}
int stack_push(Stack *s, int value)
{
if (s->count == s->capacity) return -1;
s->items[s->count++] = value;
return 0;
}/* main.c — a caller. It never sees struct Stack's fields.
* cc main.c stack.c -o demo
*/
#include <stdio.h>
#include "stack.h"
int main(void)
{
Stack *s = stack_create(4);
if (s == NULL) {
fprintf(stderr, "out of memory\n");
return 1;
}
for (int i = 0; i < 5; i++) {
if (stack_push(s, i) != 0)
printf("push %d rejected: full\n", i); /* prints for i == 4 */
}
stack_destroy(s); /* every create has a matching destroy */
return 0;
}Common Gotchas
Never define a non-`static` function or global variable in a header
two including translation units both define the symbol and the link fails with `multiple definition`. Use `extern` in the header plus one definition in a `.c` file, or mark small helpers `static inline`.
A header that only works when included second is broken
it is relying on the caller's earlier includes. Include your own header as the first line of its `.c` file, or compile it alone, to catch missing dependencies before someone else does.
`#include` is textual, so include order can change meaning
a macro defined before an include will be expanded inside it. This is how feature-selection macros like `_POSIX_C_SOURCE` work, and also why defining a macro named `max` before including a header can break that header.
Declaring the interface twice invites drift
if you hand-copy a prototype into a `.c` file instead of including the header, the compiler can no longer compare the two and a mismatch becomes undefined behavior at run time.