Skip to content

C Header Files

How to use C Header Files to separate a program into multiple files

Simple programs can be put in a single file, but when your program grows larger, it’s impossible to keep it all in just one file.

You can move parts of a program to a separate file, then you create a header file.

A header file looks like a normal C file, except it ends with .h instead of .c, and instead of the implementations of your functions and the other parts of a program, it holds the declarations.

You already used header files when you first used the printf() function, or other I/O function, and you had to type:

#include <stdio.h>

to use it.

#include is a preprocessor directive.

The preprocessor goes and looks up the stdio.h file in the standard library, because you used brackets around it. To include your own header files, you’ll use quotes, like this:

#include "myfile.h"

The above will look up myfile.h in the current folder.

You can also use a folder structure for libraries:

#include "myfolder/myfile.h"

Let’s make an example. This program calculates the years since a given year:

#include <stdio.h>

int calculateAge(int year) {
  const int CURRENT_YEAR = 2020;
  return CURRENT_YEAR - year;
}

int main(void) {
  printf("%u", calculateAge(1983));
}

Suppose I want to move the calculateAge function to a separate file.

I create a calculate_age.c file:

int calculateAge(int year) {
  const int CURRENT_YEAR = 2020;
  return CURRENT_YEAR - year;
}

And a calculate_age.h file where I put the function prototype, which is same as the function in the .c file, except the body:

int calculateAge(int year);

Now in the main .c file we can go and remove the calculateAge() function definition, and we can import calculate_age.h, which will make the calculateAge() function available:

#include <stdio.h>
#include "calculate_age.h"

int main(void) {
  printf("%u", calculateAge(1983));
}

Don’t forget that to compile a program composed by multiple files, you need to list them all in the command line, like this:

gcc -o main main.c calculate_age.c

And with more complex setups, a Makefile is necessary to tell the compiler how to compile the program.


→ Get my C Handbook

download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about clang: