How to create your own header file in C programming language

  • How can we create our own header file in C.
  • How to create your own function library in C programming language like stdio.h
Here we will create a new header file called "myMath.h" and a function "int getNearestInteger(float)" that will convert a floating point number to nearest integer and return. You can add any number of functions in a header file. Utility functions are best candidates for getting included in a header file so that we can use them in multiple programs.
    Benefits of creating your own header file having common utility functions.

  1. Code re-usability : If you added a function in a header file, then you don't have to type it again in any program where you want to use it. Just include your header file using #include preprocessor and call you function just like any other standard library function.
  2. Easy to maintain : Later, If you want to change the internal implementation of any function, then you have to modify only in one place(inside header file). You don't have to do any change in any of the client(programs who calls this function) of this function as long as the function prototype remains same.
Here are the steps to create your own header file
  • Open a text editor and type a function definition, like we define a new function in C program.
    int getNearestInteger(float N){
       return (int)(N < 0 ? N - 0.5 : N + 0.5);
  • Save this file with .h extension. Lets assume we saved this file as myMath.h.
  • Copy myMath.h header file to the same directory where other inbuilt header files are stored.
  • Compile this file.
  • To Include your new header file in a c program used #include preprocessor directive.
    #include "myMath.h" 
  • Now you can directly call any function define inside myMath.h header file.
#include <stdio.h>
#include "myMath.h" 

int main(){
    float number;
    printf("Enter an floating point number\n");
    scanf("%f", number);
    printf("Nearest Integer of %f is %d\n", number, 
    return 0;
Enter an floating point number
Nearest Integer of 2.3 is 2