Header files are also called library of C, C++, which include definition of C, C++ built in functions. To create a header file in C, C++, we are using TURBOC as editor. First we need to create a file including macro or function and save it with .h extension in include folder of TC, (eg: c:/tc/bin/head.h).
code : head.h
void fun()
{
cout<<“Best Programming Institute in delhi”;
}
and after that we can include header file in our program,
code : file.cpp
#include<iostream.h>
#include<head.h>
void main()
{
cout<<“we are learning how to make header file in c++ \n”;
fun();
}
Thank you
Team VSIT
https://www.vsit.in
How to Create Header File in C and C++
Header files are an important part of C and C++ programming. They help programmers organize code, reuse functions, and improve project management. Header files usually contain function declarations, macros, constants, and class definitions that can be shared across multiple source files.
Learning how to create a header file in C and C++ is very useful for beginners because it improves code structure and makes programs easier to manage.
What is a Header File?
A header file is a file with the .h extension that contains declarations and definitions used in multiple source files.
Header files commonly include:
- Function declarations
- Macro definitions
- Constants
- Structure declarations
- Class declarations (in C++)
Header files help reduce code duplication and improve program organization.
Why Use Header Files?
Header files are used to:
- Reuse code in multiple files
- Organize programs properly
- Improve readability
- Make large projects easier to manage
- Separate declarations from implementation
They are especially useful in modular programming.
Syntax of Header File
A simple header file syntax:
// myheader.h
void display();
The function declaration can then be used in other source files.
Steps to Create Header File in C
Step 1: Create a Header File
Create a file with the .h extension.
Example:
// myheader.h
void message();
Step 2: Create a Source File
Create a .c file and define the function.
// myheader.c
#include <stdio.h>
void message() {
printf("Welcome to Header File Example");
}
Step 3: Include Header File in Main Program
// main.c
#include <stdio.h>
#include "myheader.h"
int main() {
message();
return 0;
}
Output
Welcome to Header File Example
How Header Files Work
The #include directive is used to include the header file inside the program.
Example:
#include "myheader.h"
This tells the compiler to use declarations from the header file.
A simple include representation:
#include\ \rightarrow\ Header\ File\ \rightarrow\ Function\ Access
This process allows programs to access reusable declarations and functions.
Creating Header File in C++
Header files are also used in C++ programming.
Example:
Header File
// student.h
class Student {
public:
void show();
};
Source File
// student.cpp
#include <iostream>
#include "student.h"
using namespace std;
void Student::show() {
cout << "Header File in C++";
}
Main Program
// main.cpp
#include "student.h"
int main() {
Student s;
s.show();
return 0;
}
Advantages of Header Files
- Improves code organization
- Supports code reusability
- Reduces duplicate code
- Makes large projects easier to manage
- Simplifies maintenance
Header files are very important in modular programming.
Difference Between Header File and Source File
| Feature | Header File | Source File |
|---|---|---|
| Extension | .h | .c or .cpp |
| Contains | Declarations | Function definitions |
| Purpose | Code sharing | Program implementation |
| Reusability | High | Limited |
Important Rules While Creating Header Files
Use Include Guards
Include guards prevent multiple inclusion errors.
Example:
#ifndef MYHEADER_H
#define MYHEADER_H
void display();
#endif
Use Meaningful Names
Header file names should describe their purpose clearly.
Keep Only Declarations
Function definitions are usually placed in source files.
Common Mistakes Beginners Make
- Forgetting include guards
- Writing full function definitions in header files
- Incorrect file extensions
- Not including the header file properly
- Using wrong file paths
Avoiding these mistakes improves program structure and compilation.
Importance of Modular Programming
Header files support modular programming.
A modular programming structure:
Main\ Program\ \rightarrow\ Header\ File\ \rightarrow\ Source\ File
This structure improves program management and readability.
FAQ’s
What is a header file in C?
A header file is a .h file that contains declarations and definitions used across multiple source files.
Why are header files used?
Header files are used for code reusability and better program organization.
What is the extension of a header file?
The extension of a header file is .h.
What is #include in C?
The #include directive is used to include header files in a program.
Are header files used in C++?
Yes, header files are widely used in C++ programming.
Conclusion
Header files are an important part of C and C++ programming. They help organize code, improve reusability, and make programs easier to manage. Learning how to create and use header files is essential for beginners and software development projects.
Understanding header files also helps students learn modular programming and professional coding practices in C and C++.

thankyou sir ………………
What is Macro?????????
to define a macros in c, c++ we are using #define preprocessor. The advantage of a macro is that the it can replace repeated statement.
for example :
define SQR(x) x*x
now we can call SQR(x) macro anywhere in our program
It’s the best institute for me.