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.
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:
- Pre – Defined Header Files in the Compiler Library
- 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 in 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 know 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
int division(int a, int b)
result = a / b;
- 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 in 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.
int x, y, result;
printf("Enter First Number:\t");
printf("Enter Second Number:\t");
result = division(x, y);
- 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>, you 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.
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.