Header Files in C++ (original) (raw)

Last Updated : 29 May, 2026

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.

header_files

Header Files in C++

Contents of a Header File

Header files contain reusable programming components that help organize and simplify C++ programs.

#include // For math functions like sqrt() and pow() #include

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

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:

#include #include #include #include #include #include

using namespace std;

int main() { // Using 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-**definedheader 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 " ".

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:

// 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:

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

// 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:

#include "mathutils.h" #include

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

`

**Explanation:

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