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:
- Pre – defined header files
- 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.
int division(int a, int b)
result = a / b;
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.
int x, y, result;
printf("Enter First Number:\t");
printf("Enter Second Number:\t");
result = division(x, y);
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.