Create Your Own Header File C Program

 

Learn us understand what are header files, why do we use them and how to create your own header file in C programming.

What are header files?

A header file consists of function definitions, declarations and macros that can be used in different C programs.

 

These files are used to eliminate redundancies, inconsistencies and save on time consumption for writing repetitive lines of codes.

A #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 that are usually used:

  1. Pre – defined header files
  2. Programmer – defined header files

Why do you need header files?

When you are working on a very large scale project in C programming, you have to write a lot of functions.

There could be a high possibility that you may need one function in multiple .c files. Now, you can either write the functions again and again in these multiple .c files. However, it is difficult and time-consuming to write the functions in every file separately.

Must Read: Lvalue Required Error Solution

Also, if there’s any other change in these functions then you will have to go through all these files and manipulate that function.

Instead of all this, you can write your function in a separate .c file and include it in every .c program that uses the same function.

This .c file that contains the function can be included in any C program that uses the function defined in this header C program file. All you need to do is to do a #include<header_file.h>.

Note: This C program for creating your own header files in C programming is compiled with GNU GCC compiler on Linux Ubuntu 14.04 operating system.

Step 1: C Program To Create A Header File

1. Open the Notepad / gEdit or any other text editor(preferably gEdit).

   

2. Type the following function which is used to divide two integers in C programming language.

3. Save this file with a .h extension. Let us save this file with a name – divi.h.

Step 2: C Program To Include Header File

1. Open a Notepad / gEdit or any other text editor.

2. Type the following code in this new program. This code will take two numbers from the user and pass it to the division function which is defined in the div.h custom header file.

3. Save this C program with a .c extension. Let us save this file with a name – a.c.

Output

C Program To Write Your Own Header File in Simple Steps in Linux and Windows

Header File Facts

If you have created your 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 give an error.

If you include <divi.h>, it tells the compiler to search for the divi.h 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.

Let’s discuss more on writing custom header files in C programming in the comment section below if you have any compilation errors and any doubts about the same.

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?

    Reply
    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.

      Reply
  2. Mayank Rathor

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

    Reply
    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.

      Reply
  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.

    Reply
  4. Kapil Soni

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

    Reply
  5. Armaan Malik

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

    Reply
  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?

    Reply
  7. Vishal Sharma

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

    Reply
    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.

      Reply

Let's Discuss