Javatpoint Logo
Javatpoint Logo

How to create your own header files in C

In this article, you will learn how to create your own header files in C with the help of given steps and examples.

It is standard practice in C to create your header file to declare functions, data structures, constants, and other declarations that can be shared across numerous C source files (.c files). Your code will be more streamlined and organized, allowing for easier maintenance and promotion of reuse. How to create your own header file in C is described in the following steps:

Step-1: Make a new file with the ".h" extension.

First, name a new text file with the extension ".h" to get started. For instance, you may call it "my header.h".

Step-2: Add header guards

Header guards stop the header file from being included more than once in a single compilation unit. They make sure that the header file's contents are only included once. Add the following lines to your header file's beginning and end of your header file.

Step-3: Add your Declarations

Within the header files, you may place any declarations you want to use consistently throughout your source files. Header files usually contain declarations like function prototypes, struct definitions, constant values, and extern variables.

Let's use the following example to illustrate how to square a number and get the speed of light using a function:

Step 4: Include the Header in your source files

Include the header file at the beginning of each source file to use the declarations from your header file in your C source code.

For example, you can perform the following to include your "myheader.h" source file in your "main.c" source file:

Step-5: Compile your code

Make sure that you include your header file and all other required source files when compiling your C code. For instance, you can compile "main.c" and "myheader.h" simultaneously as follows:

Step-6: Run the code

Step-7: Output


  • The function prototype for calculateSquare and the external definition of the constant variable SPEED_OF_LIGHT are found in the header file "header.h".
  • The "my functions.c" contains an implementation of the function calculateSquare that computes the square of a specified number.
  • The definition of the constant variable SPEED_OF_LIGHT in "constants.c" is 0, and the speed of light is measured in meters per second.
  • The main function uses the header file in "main.c", which uses the constant variable SPEED_OF_LIGHT and the function calculateSquare.
  • After the source files are combined, the executable "my_program" is produced.
  • Upon execution, the program uses the calculateSquare function to get the square of the integer 5, displaying the result and the value of the constant variable SPEED_OF_LIGHT.
  • Your code has been successfully implemented using separate source files for functions and constant variables.

Complexity Analysis:

Time Complexity:

  • This function's time complexity is O(1). The function calculates the square of the supplied number num using a single operation (multiplication). The number of operations is constant regardless of the size of the num, making the time complexity O(1).

Space Complexity:

  • This function's space complexity is O(1). There are no dynamic memory allocations or recursive function calls in the function; instead, a fixed amount of memory is used to store the local variable num. Hence, the space complexity is constant.
  • The constant variable SPEED_OF_LIGHT is a double with an O(1) space complexity. The constant variable has a fixed memory footprint independent of any inputs or problem size.
  • The main function in "main.c" also has an O(1) space complexity. There are no recursive calls or dynamic memory allocations in the main function, which stores local variables like num and square in a fixed amount of memory.
  • As the complete program only utilizes a fixed amount of memory that does not change depending on the input size, its overall space complexity is also O(1).


Let's take a program to understand how to create own header files in C.


The value of PI: 3.14159
The value of (10 + 7): 17
The value of (20 - 9): 11
The value of (8 * 6): 48
The value of (12 / 3): 4.00


  • The program includes the standard input-output library h to use functions like printf.
  • The program defines a header file "operations.h" using preprocessor directives #ifndef, #define, and #endif. It prevents multiple header file inclusions in the same compilation unit.
  • Inside the header file, we have the following actions:
  • A constant declaration # defines PI 3.14159 and a macro PI with the value 14159.
  • It contains prototypes for the four operations of addition, subtraction, multiplication, and division.
  • After that, we put the four functions addition, subtraction, multiplication, and division into effect below the header file. Every function executes the appropriate arithmetic operation and returns the output.
  • The following actions are carried out in the main function:
  • Print the value of PI using the printf() function with format specifier %f to display the constant value with five decimal places.
  • Call the add, subtract, multiply, and divide functions with different arguments and print their results using the printf() function.
  • Finally, the program returns 0 to indicate successful execution.

Complexity Analysis:

Time Complexity: O(1)

The time complexity is constant because all functions carry out constant-time operations regardless of the input size.

Space Complexity: O(1)

The program utilizes a fixed amount of memory that is independent of the size of the input. Hence, the space complexity is fixed.


There are several characteristics of header files. Some main characteristics are as follows:

Simple User Interaction: The program illustrates simple user interaction by using the printf() function to display the results of mathematical operations. This feature lets users interact with the program and see the calculated results. Many programs depend on simple user interaction to give consumers feedback and useful information.

Reusability: It is aided by using a unique header file with function prototypes. Functions can be declared in the header so that other source files can be used without being redefined. This function encourages the "Don't Repeat Yourself" (DRY) maxim, preventing code duplication and fostering a cleaner, easier-to-maintain codebase. Reusability reduces code duplication and simplifies future code modifications when different program areas need the same functionality.

Flexibility: The modular design and use of functions in the program contribute to its flexibility. Adding or modifying functionalities is relatively straightforward due to the separation of concerns. New arithmetic operations or constants can be easily introduced by adding them to the header file and implementing them in the corresponding source file. This flexibility enables the program to evolve and adapt to changing requirements without the need for extensive code modifications.

Simple Execution: The program's execution is simple, making it appropriate for use in situations where the input is tiny and fixed in size. The code is simpler to comprehend and maintain for developers since it removes needless complexity and concentrates on basic arithmetic operations. The simple execution also helps debug and locate any problems in the program.

Modularization: The C program simplifies modularization using a unique header file called "operations.h" to define constants and function prototypes. The header file serves as an interface by separating the declarations from the implementations. With this modular approach, developers can separately handle various parts of the codebase, improving the organization and maintainability of the code. Modularization also assists in isolating changes as a program expands, which reduces the risk of introducing faults in other areas of the code.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA