How to Create a Custom Header File in C

By Dan Gookin

As multi-module projects in C grow more complex, you find the first part of each source code file growing longer and longer: More prototypes, more constants, and more global variables and structures are required for each module. Rather than burden your code with redundancies, you can create a header file for the project.

A header file contains just about everything you can put into a source code file. Specifically, you should put items in the header file that would otherwise go into every source code module. Here is a sample header file:

HEADER FILE EX2403.H

#include <stdio.h>
#include <stdlib.h>
/* prototypes */
void fillstructure(void);
void printstructure(void);
/* constants */
/* variables */
struct thing {
  char name[32];
  int age;
  };
typedef struct thing human;

The header file shown in Header File ex2403.h starts with some include directives, which is fine; as long as those header files are required by each module in the program, you can specify them in your own header file. Some programmers choose to do so; others do not.

Two prototypes are specified at Lines 6 and 7. Again, one reason for having a header file is to prototype, especially across multiple modules.

The header file ex2403.h lacks constants, though placing these items in a header file is quite common. There is a comment at Line 9 in case the program grows constants later.

Finally, the structure thing is defined at Line 13. Then Line 18 uses typedef so that the word human (instead of struct thing) can be used in the code.

Other popular items to include in a header file are macros. These are preprocessor directives that can also help simplify your code.

To use a local header file in your code, you specify it on a line, just like any other header file. The big difference is that double quotes are used instead of angle brackets. For example:

#include "ex2403.h"

The compiler looks for the header filename in double quotes in the current directory, along with the source code file(s). If the file isn’t in that directory, you need to specify a pathname, as in

#include "headers/ex2403.h"

Project ex2403 main.c Source Code demonstrates how the header file in Header File ex2403.h is used.

PROJECT EX2403 MAIN.C SOURCE CODE

#include "ex2403.h"
human person;
int main()
{
  fillstructure();
  printstructure();
  return 0;
}
void fillstructure(void)
{
  printf("Enter your name: ");
  fgets(person.name,31,stdin);
  printf("Enter your age: ");
  scanf("%d",&person.age);
}
void printstructure(void)
{
  printf("You are %sn",person.name);
  printf("And you are %d years old.n",person.age);
}

Line 1 of the source code shown in Project ex2403 main.c Source Code includes the custom header file, ex2403.h. The typedef human is then used at Line 3. That’s it! No other declarations are necessary in the source code because they’ve been handled by the custom header.

Exercise 1: Create a new project in Code::Blocks. Create a new header file, ex2403.h, for the project, and copy the code from Header File ex2403.h into that file. Copy the source code from Project ex2403 main.c Source Code into the main.c file. Build and run.

Exercise 2: Split out the fillstructure() and printstructure() functions from Project ex2403 main.c Source Code so that each appears in its own source code file, input.c and output.c, respectively. Build the multi-module program.