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:
- 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 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
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 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.
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>, 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.
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.