C program to create and include custom header file

Write a C program to create and include your custom header file. How to create and include your own header file in C programming. C program to include another C program and custom header files.

In this article I will explain how to create and include custom header file to a C program. 

Required knowledge

Basic C programming, C preprocessor directives

What is C header file?

C header file is a normal C file that ends with .h extension. It contains function declarations and macros

There are two types of header file, one that we create based on our requirement and the other that ships with the C compiler. From our first hello world C program to today’s program we have used a lot of built in header files. 

To use any header file we include it to our C program using #include  directive. Inclusion of a header file is similar to copying contents of header file to our program.

How to create and link a header file?

A C header file must only contain function declarations and macros. In this example we will create a custom header file for arithmetic operations. We will declare functions to perform all basic arithmetic operations in header file. Later define those functions in separate C file. Finally, include and use them in our main C file. So, lets get started.

Step by step descriptive logic to create a custom header file in C programming.

  1. Create a new file name it as arith.h this is our header file.
  2. Declare all your functions and macros in the arith.h header file. For example:
    float sum (float, float);
    float sub (float, float);
    float div (float, float);
    float mult (float, float);
    int mod (int, int);
  3. Make sure you do not accidentally declare above functions again and again. To do so lets wrap all above declarations within a include guard. 
    // If arith.h is not included to any file
    // Include guard
    #ifndef ARITH_H
    
    // Declare macro as a flag that specifies arith.h is included
    #define ARITH_H
    
    // Declare all functions
    float sum (float, float);
    float sub (float, float);
    float div (float, float);
    float mult (float, float);
    int mod (int, int);
    
    #endif
  4. Ok so we are done with the declarations part. Next, we need to define these functions. To define these functions let us create a new C file with name arith.c
  5. Define all functions declared in the header file.
    /**
     * arith.h header file function definitions.
     * Define all functions here, that are declared in arith.h.
     */
    
    float sum (float x, float y)
    {
        return (x + y);
    }
    
    float sub (float x, float y)
    {
        return (x - y);
    }
    
    float div (float x, float y)
    {
        return (x == 0 || y == 0) 
                ? 0
                : (x / y);
    }
    
    float mult (float x, float y)
    {
        return (x * y);
    }
    
    int mod (int x, int y)
    {
        return (x % y);
    }
  6. Yippie, our header file is ready. We can use this header file in our C program. To include and start using these functions in any other C file, just include this header file using #include "arith.h"

    Note: I have assumed that both your main file and header file exists in same directory. If not change the #include path.

  7. Run gcc main.c arith.c to compile main and header file.

    Note: main.c file is where we have included our header file

Program to create and include and use custom header file

File: arith.h

// If arith.h is not included to any file
#ifndef ARITH_H

// Declare macro as a flag that specifies arith.h is included
#define ARITH_H

// Declare all functions
float sum (float, float);
float sub (float, float);
float div (float, float);
float mult (float, float);
int mod (int, int);

#endif

File: arith.c 

/**
 * arith.h header file function definitions.
 * Define all functions here, that are declared in arith.h.
 */

float sum (float x, float y)
{
    return (x + y);
}

float sub (float x, float y)
{
    return (x - y);
}

float div (float x, float y)
{
    return (x == 0 || y == 0) 
            ? 0
            : (x / y);
}

float mult (float x, float y)
{
    return (x * y);
}

int mod (int x, int y)
{
    return (x % y);
}

File: main.c 

/**
 * C program to create and include custom header file
 */

#include <stdio.h>
#include "arith.h"   // Include our custom header file

int main()
{
    printf("sum(10, 20)  = %.2f\n", sum(10, 20));
    printf("sub(10, 20)  = %.2f\n", sub(10, 20));
    printf("mult(10, 20) = %.2f\n", mult(10, 20));
    printf("div(10, 20)  = %.2f\n", div(10, 20));
    printf("mod(10, 20)  = %d\n",   mod(10, 20));

    return 0;
}

To compile above program run gcc main.c arith.c

Output

sum(10, 20)  = 30.00
sub(10, 20)  = -10.00
mult(10, 20) = 200.00
div(10, 20)  = 0.50
mod(10, 20)  = 10

Happy coding 😉