Create your own Header File in C

Instead of writing a large and complex code, we can create our own header files and include them in our program to use whenever we want. It enhances code functionality and readability. Below are the steps to create our own header file:

Step 1: Write your own C code and save that file with the “.h” extension. Below is the illustration of the header file:

C




// Function to find the sum of two
// numbers passed
int sumOfTwoNumbers(int a, int b)
{
  return (a + b);
}


Step 2: Include your header file with “#include” in your C program as shown below: 

C




// C++ program to find the sum of two
// numbers using function declared in
// header file
#include "iostream"
 
// Including header file
#include "sum.h"
using namespace std;
 
// Driver Code
int main()
{
 
    // Given two numbers
    int a = 13, b = 22;
 
    // Function declared in header
    // file to find the sum
    printf("Sum is: %d", sumoftwonumbers(a, b));
}


Output

Sum is: 35

Header Files in C

In C language, header files contain a set of predefined standard library functions. The .h is the extension of the header files in C and we request to use a header file in our program by including it with the C preprocessing directive “#include”.

C Header files offer the features like library functions, data types, macros, etc by importing them into the program with the help of a preprocessor directive “#include”.

Similar Reads

Syntax of Header Files in C

We can include header files in C by using one of the given two syntax whether it is a pre-defined or user-defined header file....

Example of Header File in C

The below example demonstrates the use of header files using standard input and output stdio.h header file...

Types of C Header Files

...

Create your own Header File in C

There are two types of header files in C:...

Including Multiple Header Files

...