Reading history

Header Files in C: Creating and Using .h Files

6 min read 0 views

Learn what header files are, the difference between <> and "" includes, how to write your own header with include guards, and what belongs in one.

A header file is a .h file that tells other C files what a module offers: its function prototypes, types and constants. When you write #include <stdio.h>, you are using a header to learn that printf exists. Writing your own headers is how a program grows from one file into many tidy parts.

In this lesson you will learn

  • What goes in a header and what stays in the .c file
  • The difference between #include <...> and #include "..."
  • How to write a header with an include guard
  • How to build a program from several files
  • The mistakes that cause “undefined reference” and “redefinition” errors

What Header Files Are For

Before C lets you call a function, it wants to know the function’s name, its parameters and its return type. That information is a declaration. If a function lives in another file, you would have to retype its declaration in every file that uses it. A header file stores those declarations once, and #include pastes them in wherever they are needed.

So a module is usually split in two:

  • stats.h: the promise. It says what the module can do.
  • stats.c: the work. It holds the function bodies.

Standard Headers vs Your Own

FormUsed forWhere the compiler looks
#include <stdio.h>Standard and system headersThe system include folders
#include "stats.h"Your own headersFirst the folder of the current file, then the system folders

A few standard headers you will use often: stdio.h for input and output, stdlib.h for malloc and exit, string.h for string functions, math.h for maths, and time.h for dates and times.

Writing and Using Your Own Header

Here is a small statistics module. First the header. It holds only declarations, wrapped in an include guard:

#ifndef STATS_H
#define STATS_H

#include <stddef.h>

/* Declarations only: what the functions look like, not how they work. */
double stats_mean(const int *values, size_t n);
int    stats_max(const int *values, size_t n);

#endif /* STATS_H */

The guard works like this. The first time the file is included, STATS_H is not defined, so the preprocessor defines it and keeps the contents. If the same file is included again, STATS_H already exists, so everything up to #endif is skipped.

Next, the source file. It includes its own header so the compiler can check that the bodies match the promises:

#include "stats.h"

double stats_mean(const int *values, size_t n) {
    long sum = 0;
    for (size_t i = 0; i < n; i++) sum += values[i];
    return n ? (double)sum / n : 0.0;
}

int stats_max(const int *values, size_t n) {
    int best = values[0];
    for (size_t i = 1; i < n; i++)
        if (values[i] > best) best = values[i];
    return best;
}

Finally, a program that uses the module. Notice the header is included twice by mistake, and nothing breaks:

#include <stdio.h>
#include "stats.h"
#include "stats.h"   /* included twice by mistake: the guard makes it harmless */

int main(void) {
    int runs[] = {45, 12, 78, 33, 60};
    size_t n = sizeof runs / sizeof runs[0];
    printf("mean = %.1f\n", stats_mean(runs, n));
    printf("max  = %d\n", stats_max(runs, n));
    return 0;
}

Compile each .c file, then link them together:

gcc -std=c11 -Wall -Wextra -c stats.c -o stats.o
gcc -std=c11 -Wall -Wextra main.c stats.o -o report
./report

Output

mean = 45.6
max  = 78

You never compile the .h file on its own. It only exists to be included. To package stats.o so other projects can use it, see creating a library in C.

What Belongs in a Header

  • Yes: function prototypes, struct and enum definitions, typedefs, macros, extern declarations of shared variables, and static inline helper functions.
  • No: ordinary function bodies and variable definitions such as int count = 0;. If two .c files include them, the linker sees two copies and fails.

Include Guard and Definition Mistakes

  1. No include guard. Including the header twice (often through another header) repeats a struct definition, which gives a “redefinition” error.
  2. The same guard name in two headers. The second header is silently skipped. Base the name on the file name, like STATS_H.
  3. A variable defined in a header. Gives “multiple definition” at link time. Put extern int count; in the header and int count = 0; in one .c file. See linkage in C.
  4. Forgetting to link the .c file. The header compiles fine, but the linker reports “undefined reference to stats_mean”. Add stats.c or stats.o to the build.
  5. Including a .c file. #include "stats.c" may work in a tiny program but causes duplicate definitions as soon as two files do it.

Practice Exercises

Exercise 1. Write the include guard lines for a header called shapes.h.

Show answer
#ifndef SHAPES_H
#define SHAPES_H
/* declarations here */
#endif

Exercise 2. Two files include config.h, which contains int level = 1;. What error appears, and how do you fix it?

Show answer

The linker reports a multiple definition of level. Change the header to extern int level; and define int level = 1; in exactly one .c file.

Exercise 3. Should you write #include <stats.h> or #include "stats.h" for your own file?

Show answer

"stats.h". The quoted form searches the current file’s folder first, which is where your own headers usually are.

Frequently Asked Questions

Do I need a header file for a one-file program?

No. Headers matter once you have two or more .c files that share functions or types. A small single-file program only needs the standard headers it uses.

What is the difference between a header file and a library?

A header only declares functions. The library (or .c / .o file) holds the compiled code. You need the header to compile and the library to link.

Is #pragma once better than an include guard?

#pragma once is shorter and works on GCC, Clang and MSVC, but it is not part of the C standard. Classic #ifndef guards work everywhere.

Can a header include other headers?

Yes. A header should include whatever it needs to compile on its own, such as stddef.h for size_t. Guards stop the repeats from causing trouble.

Summary

  • A header shares declarations; the matching .c file holds the code.
  • Use <> for standard headers and "" for your own.
  • Every header needs an include guard with a unique name.
  • Keep function bodies and variable definitions out of headers.
  • Compile each .c file and link them all together.
Lessons are also marked complete when you click Next.