Header Files in C++

Header files are files that contain a set of predefined functions which we can import by using the preprocessor directive #include. The preprocessor directive tells the compiler that the header file needs to be processed before the compilation.

Header files are declared at the top of the program. Every C++ program needs a header file to run the program. Header files are used to reduce the complexity and number of lines of code, we don’t have to write code for every thing.

A header file in C++ contains:

  • Function definitions
  • Data type definitions
  • Macros

Function Definitions

Header files contain a the set of predefined functions that can be used by simply including the header file in our program. For example, pow(x, y) function defined in the cmath header file that can be imported in a program to find the power of any integer value.

Example

#include <iostream>
#include <cmath>
using namespace std;

int main() {
  cout << pow(6, 2);
  return 0;
}
Output
36

Data Type Definition

Header files also have some data type definitions which are frequently used in the program. For example, int is an integer data type used to represent an integer number with no fractional part.

Example

int age = 13;
Output

Macros

Macro is a piece of code that gets replaced by the value of the macro. Macros are defined by the #define directive. When the compiler sees the macros in the program, it replaces the value with the value contained in macros.

Example

#include <iostream>
using namespace std;

#define PI 3.1416

int main() {
  float radius = 5;
  float area;
  area = PI * radius * radius;
  cout << "Area is " << area;
  return 0;
}
Output
Area is 78.54

Types of Header Files in C++

There are of 2 types of header file:

  1. Pre-existing Header Files
  2. User-defined Header Files

Pre-existing Header Files

These header files are already available in C++ standard library compiler; we just need to import them. They are generally included in the program with the angular brackets.

Example

#include <iostream>

User-defined Header Files

These header files are defined by the user and can be included in the program using double quotes.

Example

#include "filename.h"

To create your own header file, follow the below steps:

  1. Create a file with ".h" extension. Let the file name be sum.h.
  2. Next, add the following code to the file:
  3. 
        int sumOfTwoNumbers(int a, int b)
        {
            return (a + b);
        }
        
  4. Save the above code.
  5. Now, you can include the above header file using #include in your program as shown below:
  6. 
    #include <iostream>
    #include "sum.h" //including user define header file
    using namespace std;
     int main()
    {
       int a = 9, b = 19;
       cout << "Sum is " << sumOfTwoNumbers(a, b);
    }
    
    Output
    Sum is 28

How Do Header Files Work?

When you include a header file, like #include<cmath> in the program, it tells the compiler that it possesses this function and functionality, and the compiler replaces the <cmath> with its actual functionality. For instance, we can import the cmath header file into the program and use the round() function if we need a function to remove decimal values from a floating number. In this manner, we avoid having to create our own code in order to eliminate decimal numbers from a floating value.

Example

#include <iostream>
#include <cmath>
using namespace std;

int main() {
  cout << round(234.3423);
  return 0;
}
Output
234