Header files in C++ contain reusable declarations such as functions, classes, variables, and macros. They help organize programs, improve code reusability, and make large projects easier to manage.
- Allow code reuse across multiple source files
- Included in programs using the #include directive
- Help divide programs into smaller and manageable modules

Contents of a Header File
Header files contain reusable programming components that help organize and simplify C++ programs.
- Function Definitions: Function definitions contain the actual implementation of functions used in a program and help reuse commonly used functionality.
- Data Type Definitions: Data type definitions specify the type and structure of data used in a program and improve readability and data organization.
- Macros: Macros are preprocessor directives used for text substitution before compilation and help reduce repetitive code.
#include <cmath> // For math functions like sqrt() and pow()
#include <iostream>
using namespace std;
int main()
{
// Using math functions
double sqrt_res = sqrt(25);
double pow_res = pow(2, 3);
// Display results
cout << "Square root of 25: " << sqrt_res << endl;
cout << "2^3: " << pow_res << endl;
return 0;
}
Output
Square root of 25: 5 2^3: 8
Types of header files
Header files in C++ are mainly classified into two categories based on their usage and source.
Standard Header Files/Pre-existing header files
These are built-in header files that come with the C++ standard library. They provide commonly used functions and classes, so we don't have to write everything from scratch. Some commonly used standard header files are:
- <iostream>: It contains declarations for input and output operations using streams, such as std::cout, std::cin and std::endl.
- <cmath>: It is used to perform mathematical operations like sqrt(), log2(), pow(), etc.
- <cstdlib>: Declares functions involving memory allocation and system-related functions, such as malloc(), exit(), and rand().
- <cstring>: It is used to perform various functionalities related to string manipulation like strlen(), strcmp(), strcpy(), size(), etc.
- <vector>: It is used to work with container class for dynamic arrays (vectors) functions like begin() , end().
- <string>: Provides the std::string class and functions for string manipulation.
- <iomanip>: It is used to access set() and setprecision() function to limit the decimal places in variables.
- <cerrno>: It is used to perform error handling operations like errno(), strerror(), perror(), etc.
- <ctime>: It is used to perform functions related to date() and time() like setdate() and getdate(). It is also used to modify the system date and get the CPU time respectively.
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
// Using <iostream>
cout << "Hello, Geek!" << endl;
// Using <cmath>
double squareRoot = sqrt(25);
cout << "Square root of 25: " << squareRoot << endl;
// Using <cstdlib>
int randomNum = rand() % 100; // Random number between 0 and 99
cout << "Random number: " << randomNum << endl;
// Using <cstring>
char str1[20] = "Hello";
char str2[] = " World";
strcat(str1, str2);
cout << "Concatenated string: " << str1 << endl;
// Using <vector>
vector<int> numbers = {1, 2, 3, 4, 5};
cout << "Vector elements: ";
for (int num : numbers)
{
cout << num << " ";
}
cout << endl;
// Using <string>
string greeting = "Hello, ";
string name = "Programmer";
string fullGreeting = greeting + name;
cout << "Greeting message: " << fullGreeting << endl;
return 0;
}
Output
Hello, Geek! Square root of 25: 5 Random number: 83 Concatenated string: Hello World Vector elements: 1 2 3 4 5 Greeting message: Hello, Programmer
User-defined header files
User-defined header files are custom header files created by programmers according to the requirements of their programs. These files are included using #include " ".
- Used to store user-defined functions, classes, variables, and declarations in separate files
- Help organize large programs by separating reusable code from the main source file
- Improve code readability, modularity, and maintainability
- Allow the same code to be reused across multiple source files
Creation of User-defined Header Files
Instead of writing a large and complex code, we can create your own header files and include them in our program to use it whenever we want. It enhances code functionality and readability. Below are the steps to create our own header file:
Step 1: Create a Header File
- Create a new file.
- Save it as mathutils.h.
// If MATHUTILS_H is not defined, define it (start of header guard)
#ifndef MATHUTILS_H
// Define MATHUTILS_H to prevent multiple inclusions
#define MATHUTILS_H
// Function declarations (only the function names and parameters, not the logic)
int add(int a, int b);
int multiply(int a, int b);
#endif
Explanation:
- #ifndef, #define, #endif are include guards that prevent multiple inclusion.
- Only function prototypes are written in header files.
Step 2: Create a .cpp File to Write function definitions
- Creates a file called mathutils.cpp
- Write the actual function definitions (logic).
// mathutils.cpp Include your header file
#include "mathutils.h"
// Function definitions
int add(int a, int b)
{
return a + b;
}
int multiply(int a, int b)
{
return a * b;
}
Explanation:
- The header file is included using double quotes " " for user-defined headers.
- Functions declared in the header are defined here.
Step 3: Use our header file in another .cpp file
#include "mathutils.h"
#include <iostream>
int main()
{
std::cout << "Add: " << add(5, 3) << std::endl;
std::cout << "Multiply: " << multiply(4, 6) << std::endl;
return 0;
}
Explanation:
- The compiler knows about the functions through the header file.
- Actual function code is linked from math_utils.cpp.
Note: Make sure to include your self-defined header file in the source file where its functions are used, otherwise, the compiler will report an "undefined reference" error during linking.