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:
- Function definitions
- Data type definitions
- 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++
#include <cmath> // Including the standard header file for math operations
#include <iostream>
using namespace std;
int main()
{
int sqrt_res = sqrt (25);
int pow_res = pow (2, 3);
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++:
- Standard Header Files/Pre-existing header files
- 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:
It contains declarations for input and output operations using streams, such as std::cout , std::cin , and std::endl
|
It is used to perform mathematical operations like sqrt(), log2(), pow(), etc.
|
Declares functions involving memory allocation and system-related functions, such as malloc() , exit() , and rand()
|
It is used to perform various functionalities related to string manipulation like strlen(), strcmp(), strcpy(), size(), etc.
|
It is used to work with container class for dynamic arrays (vectors) functions like begin() , end().
|
Provides the std::string class and functions for string manipulation
|
It is used to access set() and setprecision() function to limit the decimal places in variables.
|
It is used to perform error handling operations like errno(), strerror(), perror(), etc.
|
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++
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
cout << "Hello, Geek!" << endl;
double squareRoot = sqrt (25);
cout << "Square root of 25 is: " << squareRoot << endl;
int randomNum = rand () % 100;
cout << "Random number is : " << randomNum << endl;
char mystr1[] = "Hello" ;
char mystr2[] = " World" ;
strcat (mystr1, mystr2);
cout << "string after concatenation: " << mystr1
<< endl;
vector< int > numbers = { 1, 2, 3, 4, 5 };
cout << "Vector elements are: " ;
for ( const auto & num : numbers) {
cout << num << " " ;
}
cout << endl;
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();
circle(320, 240, 50);
delay(2000);
closegraph();
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++
int sumOfTwoNumbers( int a, int b) { return (a + b); }
|
- step2: Include your header file with “#include” in your C++ program as shown below:
C++
#include "iostream"
#include "sum.h"
using namespace std;
int main()
{
int a = 13, b = 22;
cout << "Sum is: " << sumOfTwoNumbers(a, b) << endl;
}
|
Output
Sum is: 35
Last Updated :
11 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...