Open In App

Header Files in C++

Last Updated : 26 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A header file in C++ is a file that contains declarations (not the actual code) of functions, constants, macros, and variables that can be used in multiple programs or files.

Syntax of Header files

C++
//Including Standard Header Files
#include <header_file_name>
//Including User-Defined Header Files
#include "file_name.h"

Example:

C++
// C++ program to demonstrate the inclusion of header file.

#include <cmath> // Including the standard header file for math operations
#include <iostream>
using namespace std;

int main()
{

    // Using functions from the included header file
    // (<cmath>)
    int sqrt_res = sqrt(25);
    int pow_res = pow(2, 3);

    // Displaying the results
    cout << "Square root of 25 is: " << sqrt_res << endl;
    cout << "2^3 (2 raised to the power of 3) is: "
         << pow_res << endl;

    return 0;
}

Output
Square root of 25 is: 5
2^3 (2 raised to the power of 3) is: 8

Types of header files

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

  1. Standard Header Files/Pre-existing header files
  2. User-defined header files

1. 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:

Header File

Description

<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.

Example

The below program demonstrates the use of Standard header files.

C++
// C++ program to demonstrate Standard header files

#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 is: " << squareRoot << endl;

    // using<cstdlib>
    int randomNum = rand() % 100; // Generate a random
                                  // number between 0 and 99
    cout << "Random number is : " << randomNum << endl;

    // using <cstring>
    char mystr1[] = "Hello";
    char mystr2[] = " World";
    strcat(mystr1, mystr2);
    cout << "string after concatenation: " << mystr1
         << endl;

    // using <vector>
    vector<int> numbers = { 1, 2, 3, 4, 5 };
    cout << "Vector elements are: ";
    for (const auto& 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 is: 5
Random number is : 83
string after concatenation: Hello World
Vector elements are: 1 2 3 4 5 
Greeting message: Hello, Programmer

2. User-defined header files

These files are defined by the user and can be imported using #include" ". These are mainly used to encapsulate our own functions, classes, or declarations so that we can organize our code and reuse our code by separating it in different files.

Example

C++
#include <graphics.h>

int main()
{
    initgraph(); // Initializing graphics system
    circle(320, 240, 50); // Drawing a circle
    delay(2000); // take Pause for 2 seconds
    closegraph(); // Closing graphics system
    return 0;
}

Note The above code is specific to certain compilers and environments like Turbo C++ on MS-DOS only.

How to create your own Header File?

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
C++
// 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);   
// End of header guard
#endif                  

Step 2: Create a .cpp File to Write function definitions

  • Creates a file called mathutils.cpp
  • Write the actual function definitions (logic).
C++
// 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;
}

Step 3: Use our header file in another .cpp file

C++
#include <iostream>     // For cout
#include "mathutils.h"  // Include your custom header file

int main() {
    std::cout << "Add: " << add(5, 3) << std::endl;
    std::cout << "Multiply: " << multiply(4, 6) << std::endl;
    return 0;
}



Article Tags :
Practice Tags :

Explore