C Programming

How to Use Header Files in C

C is a versatile and powerful programming language featuring a comprehensive collection of libraries populated with predefined functions for use by its user.

This guide will look at C header files, how they work, and how to use them in our code.

What Is a Header File?

Header files are specific files containing external code that’s reusable in other programs by importing them. Typically, a C header file contains functions, data type definitions, and macros.

There are two types of header files:

  1. C standard library header files
  2. User-defined header files

The C standard headers are predefined header files readily available in the C compiler.
User-defined header files, on the other hand, are user-developed for use in a specific situation. User-defined header files are included with the #define directive.

How to Include a Header File

To use the functions, data types, and macros defined in a header file, you must import them to your program.

To import a header, use the #include, a preprocessor directive telling the compiler that it should import and process the code before compiling the rest of the code.

On a typical C program, it should contain the stdio.h header file, which is the standard header file for input and output streams.

The general syntax to import a header file is:

#include <file.h>

We enclose the header name in angle brackets.

NOTE: Ensure to include the .h extension in C programs.

It is also good to note that you can only import a header file once, and you cannot have header files with similar names, even if they contain different lines of code. That is because the compiler imports and processes both files, which leads to errors.

User-Defined Header Files

C allows you to define personal header files with custom code for your needs. This helps you organize your code and reduce complexity.

To create a custom header file, create a C file and save it with the extension .h instead of .c.

Once created, add the code you wish to include in your header and save it. For example, the following simple loop is in a header file called loopme.h:

void loop() {
    for (int i = 0; i < 10; i++) {
    printf("%d", i);

To use the header file containing the above loop, we can import it using the #include directive.
Start by creating a file. For example, program.c.

To import the header file, add the #include, and followed by the name of the file enclosed in double-quotes as:

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

NOTE: We enclose the user-defined header file with double quotes instead of angled brackets.

Once you include your header file, compile your code to execute the loop located in the header file.

Typically, you will not be including only a single loop in a header file. However, you can use it to create more complex header files.

In the resource provided, you can learn more about all the C header files.


This short tutorial discusses how C header files work, including defining and importing the files into your C programs.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list