Open In App

Header Files in C++

Improve
Improve
Like Article
Like
Save
Share
Report

C++ offers its users a variety of functions, one of which is included in header files. In C++, all the header files may or may not end with the “.h” extension unlike in C, Where all the header files must necessarily end with the “.h” extension. Header files in C++ are basically used to declare an interface of a module or any library.

A header file contains the following: 

  1. Function definitions
  2. Data type definitions
  3. Macros

It offers the above features by importing them into the program with the help of a preprocessor directive “#include”. These preprocessor directives are used to instruct the compiler that these files need to be processed before compilation. In C++ program has the header file which stands for input and output stream used to take input with the help of “cin” and “cout” respectively. 

Syntax of Header Files in C++

#include <filename.h>  // for files already available in system/default directory
or
#include "filename.h"  // for files in same directory as source file

We can include header files in our program by using one of the above two syntaxes whether it is the pre-defined or user-defined header file. The “#include” preprocessor is responsible for directing the compiler that the header file needs to be processed before compilation and includes all the necessary data types and function definitions.

Example

The below example illustrates the inclusion of header files in a C++ program.

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

Note We can’t include the same header file twice in any program.

Types of Header Files in C++

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 and their Uses

These are the files that are already available in the C++ compiler we just need to import them. Standard header files are part of the C++ Standard Library and provide commonly used functionalities. They contains the declarations for standard functions, constants, and classes.

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 and their Uses

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: 

  • step1: Write your own C++ code and save that file with “.h” extension. Below is the illustration of header file:  

C++




// Function to find the sum of two
// numbers passed
int sumOfTwoNumbers(int a, int b) { return (a + b); }


  • step2: Include your header file with “#include” in your C++ program as shown below:

C++




// C++ program to find the sum of two
// numbers using function declared in
// header file
#include "iostream"
 
// Including header file
#include "sum.h"
using namespace std;
 
int main()
{
 
    // Given two numbers
    int a = 13, b = 22;
 
    // Function declared in header
    // file to find the sum
    cout << "Sum is: " << sumOfTwoNumbers(a, b) << endl;
}


Output

Sum is: 35


Last Updated : 11 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads