• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

How to Create Header File in C Program | Example and Advantages

Aniruddha Chaudhari/17832/0
C / C++Code

If you keep writing all the code in a single file, it will make clutter. It is very difficult to maintain especially if you are working on a big project.

In this tutorial, you will learn how to create header file in c and how to call a function defined in another file by including the header file in your program.

If you have worked on any standard project, there is a high possibility that you might have seen projects having program file util.C. It is not a standard but mostly used practice to store all the essential utility functions and variables in a single file and then call it from any of the project files.

How to do that?

Here is the quick tutorial I am sharing to let you understand. You need to create three program files.

  • Writing a C program with a function definition that needs to be called from another program file
  • A header file with declarations of all the defined functions
  • C program file to call the remote functions

How to Create Header File in C Program?

Here is a step-by-step procedure with complete code and a detailed explanation with examples.

Step 1:

Write a program and define any function that needs to be called from other files.

int sumOfTwoNumbers(int num1, int num2)
{
    return num1 + num2; 
}

Save it (says util.C).

Note: You don’t need to define the function main() as you are not running this code explicitly. You only need to call the function defined in this program file.

Step 2:

Create a header file in C Program and declare all the functions defined in the above util.C program file.

int sumOfTwoNumbers(int num1, int num2);

Save the file with the same name but the extension .h (for you util.h).

Step 3:

Write a program to call the function defined in util.C file.

With all the basic header files required, you have to include util.h.

#include <stdio.h>
#include "util.h" 

int main()
{
    printf("Sum of two numbers: %d", sumOfTwoNumbers(10, 30));
    return 0; 
}

Save this file (says testProg.C)

You have added stdio.h file to use printf() function.

You might have noticed as I have included util.h file with "---" mark. While compiling code, the header file is searched in the current directory/folder rather than the C program installed repository.

Just like util.h header file we have created, there is stdio.h file already present in the GCC directory. You can use the command to find all header file locations included in your program.

So basically both the functions printf() and sumOfTwoNumbers() are defined in the remote file. To use these functions, you need to add respective header files.

Note: Store all the above three program files in the same directory.

Running & Compiling Program

The syntax for Compiling multiple files into one output file:

gcc <list_of_program_files_to_be_compiled> -0 <output_file>

In your case,

gcc util.c testProg.c -0 pro

Note: You don’t need to mention the header file while compiling the code.

Run it.

On Windows:

pro

On Linux:

You may need executable permission to execute the output file.

chmod 777 pro

Learn more about Linux file permission.

Now execute,

./pro

Output:

Sum of two numbers: 40

Advantages of Header file in C and Calling Functions from Another File

  • It preserves modularity by writing all the similar kinds of functions in a single file.
  • If you require the same kind of operation to be performed in multiple program files, it is good practice to define the function in a single file and call it from whichever program file you need. So it reduces the line of code.
  • For example, you need to sort the elements in multiple program files. You can simply write a sorting program (Selection sort, Bubble sort) in your util program and declare it in the header file. Now you can call this function from any of the program files.
  • If you need to update the code for a particular operation, you just need to update the single function definition. So it avoids the possibility of blunders.

I tried my best to make it simple. Hope you understand this tutorial to create a header file in the C program. If you have any doubts regarding the header file, let’s have a discussion in the comment section below.

cpp
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

C Programming

  1. C- Introduction
  2. C- Compile & Execute Program
  3. C- Data Types
  4. C- if-else statement
  5. C- While, do-while, for loop
  6. C- Array
  7. C- Function (Types/Call)
  8. C- strlen() vs sizeof()
  9. C- Nested Switch Statement
  10. C- Recursion
  11. C- Dynamic Programming
  12. C- Storage Classes
  13. C- Creating Header File
  14. C- Null Pointer
  15. C- Stack and Queue
  16. C- Implement Stack using Array
  17. C- Implement Linked List in C
  18. C- File Handling
  19. C- Makefile Tutorial

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

Sorting Algorithms

  • Different Types of Sorting Algo
  • Selection Sort
  • Bubble Sort
  • Quick Sort

Programming for Practice

  1. Online C/C++ Compiler

String Handling:

  1. Remove White Spaces from String
  2. Implement strstr Function in C
  3. Convert String to Int – atoi()
  4. Check if String is Palindrome
  5. Check if Two Strings are Anagram
  6. Split String in using strtok_r()
  7. Undefined reference to strrev

Array:

  1. Check if Array is Sorted

Bit Manipulation:

  1. Count Number of 1’s in Binary

Linked List:

  1. Reverse a Linked List Elements

Number System:

  1. Program to Find 2’s Complement
  2. Convert Decimal to Binary in C

Tricky Questions:

  1. Add Two Numbers without Operator
  2. Find Next Greater Number
  3. Swap values without temp variable
  4. Print 1 to 100 without Loop

Interview Coding Questions

  • 50+ Interview Coding Questions

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard