Create Your Own Header File C Program

C Program To Create Your Own Header File in Linux Ubuntu

Learn how to create your own header file in C programming in Linux Ubuntu OS. This guide helps you to make a .h file in C programming which will be your own customised header files and include it in your C Programs using the #include statement. This is the easiest article to guide on how to create a .h file in C programming language.

What are Header Files?

A header file consists of function definitions, declarations and macros that can be used in several C programs. These files are used to eliminate redundancies, inconsistencies and save on time consumption. An include statement is used to import the contents of such header files including functions and macro definitions. There are two types of header files in C programming:

  1. Pre – defined header files in the compiler library
  2. Programmer – defined header files

Why The Need of Header Files?

When you are working on a very large scale project in C programming, you have to write a lot of functions. So, obviously, there will be many people working on the same project. It is, therefore, difficult to write all the functions in a single file and keep it updating every other day.

Instead, you can write you own functions in your separate file which is known as Header Files, and then these Header Files can be included in the main program file. This technique, therefore, helps to ease the debugging, reduce the number of lines of code and thereby, save on time.

Step 1: Create A Header File in C Programming

  • Open the Notepad / gEdit or any other editor (preferably gEdit)
  • Type the following program which is used to divide two numbers in C programming

  • Save the file with a .h extension. For instance, let us save it with the name – divi.h. You can save this file at any location on your computer.

Step 2: Write a C Program using your own Header File

  • Again, open the Notepad / gEdit or any other editor
  • Type the following code in this new program. This code will take two numbers from the user and pass it to the division function.
  • Save this program code with a .c extension. For instance, let us give it a name – a.c. You can save this file at any location.

  • If you have created the header file in a location other than the include folder in C drive then you must use the above version #include”divi.h”. If you use #include<stdio.h>, your program won’t work and it will produce an error.
  • If you include <divi.h>, it tells the compiler to search for the divi.h header file in the Standard C Library Directory. The “divi.h” indicates the compiler to search for the divi.h file in the Current Directory as well as the Standard C Library. Hence, it is always a good option to use the later version with double quotes. If you save the header files in the C library directory, then you can use the first version of #include statement.
  • We have used the Division function which is defined in the file divi.h.
  • We do not need to compile or perform any other operations on the Header Files.
  • Now, simply compile and run the a.c file. You will be able to execute the program successfully.


Create Your Own Header File in C Programming in Linux Ubuntu

If you have any compilation error or doubts in this C program for creating your own header files in Ubuntu, let us know about it in the comment section below.

Recommended Programs
C Program To Find Day of a given Date
C Program To Display Map of India
C Program To Develop A Calculator Application
C Program To Count Zeros at the End in Factorial of Number
C Program To Print System Time and Date
C Program To Convert Binary Number into Decimal Value
C Program To Shutdown System For Linux Ubuntu

15 thoughts on “Create Your Own Header File C Program

  1. Mohit Thawani

    I am using Linux Ubuntu. Where can I can find the GCC Compiler’s Header Files?

    1. Tushar Soni Post author

      Since there is no need to compile header files, these are normally portable in other OS. However, there may be some implementation differences in different OS and Compilers.

  2. Mayank Rathor

    Is it compulsory to save the Header Files in C Programming Library Files in the C Drive?

    1. Tushar Soni Post author

      No. It is not mandatory. But, if you’re having multiple header files, it is generally a good idea to keep all the header files in one directory / folder.

  3. Archana Gaikwad

    This is just an awesome tutorial on creating header files in C programming. Thank you so much. It has helped me to developed a mini project in C programming. This concept of Header files is really good and helps to maintain the source code without much hassles.

  4. Kapil Soni

    Such a fantastic code. Now, I can start developing my own project. Thanks for this Header file implementation explanation.

  5. Armaan Malik

    I can finally now create new and customized header files for my c project. Thanks a lot.

  6. Shivam Sharma

    If I create a header file in Windows 7, will it work in Mac OS or do I need to make any other modifications?

  7. Vishal Sharma

    I am using Codeblocks for C programming. How can we create header files in Codeblocks?

    1. Tushar Soni Post author

      Codeblocks>New Project>File>C/C++ Header File. Write the above given code in that header file and save it in the current directory. Now, include the same header file in your C program, and run it. It should work fine.


Let's Discuss