Open In App
Related Articles

Header Files in C/C++ and its uses

Improve Article
Improve
Save Article
Save
Like Article
Like

In C language, header files contain a set of predefined standard library functions. The .h is the extension of the header files in C and we request to use a header file in our program by including it with the C preprocessing directive “#include”. C language has numerous libraries that include predefined functions to make programming easier.

C++ Language also 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.

It offers the features like library functions, data types, macros, etc 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.

Syntax of Header Files in C/C++

We can include header files in C by using one of the given two syntaxes whether it is a pre-defined or user-defined header file.

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

The “#include” preprocessor directs the compiler that the header file needs to be processed before compilation and includes all the necessary data types and function definitions.

header files example

Header Files in C

Example

C




// C program to demonstrate the use of header files
//    standard input and output stdio.h header file
#include <stdio.h>
 
int main()
{
    printf(
        "Printf() is the function in stdio.h header file");
    return 0;
}


C++




// C++ program to demonstrate the use of header files
//    standard input and output header file iostream
#include <iostream>
using namespace std;
 
int main()
{
 
    cout << "cout is a method of iostream header file";
    return 0;
}


Output

Printf() is the function in stdio.h header file

Types of Header Files

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

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

1. Standard Header Files in C and their Uses

Standard header files contain the libraries defined in the ISO standard of the C programming language. They are stored in the default directory of the compiler and are present in all the C compilers from any vendor.

There are 31 standard header files in the latest version of C language. Following is the list of some commonly used header files in C:

Header File

Description

<assert.h> It contains information for adding diagnostics that aid program debugging.
<errorno.h> It is used to perform error handling operations like errno(), strerror(), perror(), etc.
<float.h>

It contains a set of various platform-dependent constants related to floating point values. These constants are proposed by ANSI C. 

They make programs more portable. Some examples of constants included in this header file are- e(exponent), b(base/radix), etc.

<math.h> It is used to perform mathematical operations like sqrt(), log2(), pow(), etc.
<signal.h> It is used to perform signal handling functions like signal() and raise().
<stdarg.h>

It is used to perform standard argument functions like va_start() and va_arg(). It is also used to indicate start of the

variable-length argument list and to fetch the arguments from the variable-length argument list in the program respectively.

<ctype.h>

It contains function prototypes for functions that test characters for certain properties, and also function prototypes for 

functions that can be used to convert uppercase letters to lowercase letters and vice versa.
 

<stdio.h> It is used to perform input and output operations using functions like scanf(), printf(), etc.
<setjump.h>

It contains standard utility functions like malloc(), realloc(), etc. It contains function prototypes for functions that allow bypassing 

of the usual function call and return sequence.

<string.h> It is used to perform various functionalities related to string manipulation like strlen(), strcmp(), strcpy(), size(), etc.
<limits.h>

It determines the various properties of the various variable types. The macros defined in this header limits the values of 

various variable types like char, int, and long. These limits specify that a variable cannot store any value 

beyond these limits, for example, an unsigned character can store up to a maximum value of 255.

<time.h>

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.

<stddef.h> It contains common type definitions used by C for performing calculations.
<locale.h>

It contains function prototypes and other information that enables a program to be modified for the current locale on which it’s running. 

It enables the computer system to handle different conventions for expressing data such as times, dates, or large numbers throughout the world.

Example:

C




// C program to illustrate
// the use of header file
// in C
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
// Driver Code
int main()
{
    char s1[20] = "12345";
    char s2[10] = "Geeks";
    char s3[10] = "ForGeeks";
    long int res;
 
    // Find the value of 9^3 using a
    // function in math.h library
    res = pow(9, 3);
    printf("Using math.h, "
           "The value is: %ld\n",
           res);
 
    // Convert a string to long long int
    // using a function in stdlib.h library
    long int a = atol(s1);
    printf("Using stdlib.h, the string");
    printf(" to long int: %ld\n", a);
 
    // Copy the string s3 into s2 using
    // using a function in string.h library
    strcpy(s2, s3);
    printf("Using string.h, the strings"
           " s2 and s3: %s %s\n",
           s2, s3);
    return 0;
}


Output

Using math.h, The value is: 729
Using stdlib.h, the string to long int: 12345
Using string.h, the strings s2 and s3: ForGeeks ForGeeks

2. Non-Standard Header Files in C and Their Uses

Non-standard header files are not part of the language’s ISO standard. They are generally all the header files defined by the programmers for purposes like containing custom library functions etc. They are manually installed by the user or maybe part of the compiler by some specific vendor.

There are lots of non-standard libraries for C language. Some commonly used non-standard/user-defined header files are listed below:

Header File Description
<conio.h> It contains some useful console functions.
<gtk/gtk.h> It contains GNU’s GUI library for C.

Standard Header Files in C++ and their Uses

Standard header files contain libraries that are the part of C++ ISO standard. They come pre-installed with the compiler from any vendor. We can import them directly in our program using #include preprocessor.

Apart from its own standard libraries, C++ contains all the C language’s standard libraries. 

Header File Description
<iostream> It is used as a stream of Input and Output using cin and cout.
<iomanip> It is used to access the set() and setprecision() functions to limit the decimal places in variables.
<fstream> It is used to control the data to read from a file as an input and data to write into the file as an output.
<algorithm> It contains some useful algorithms which are part of STL.
<new> It contains dynamic memory allocation methods.
<vector> It contains the definition of the vector class container of STL.
<map> It contains the definition of the map class container of STL.

Example:

C++




// C++ program to demonstrate the use of standard header
// files in c++
#include <iostream>
#include <vector>
 
using namespace std;
 
// driver code
int main()
{
    // cout is defined in <iostream>
    cout << "Using iostream's cout to print" << endl;
 
    // vector defined in <vector> header file
    vector<int> v{ 11, 12, 14 };
 
    cout << "Using vector container: ";
    for (auto i : v) {
        cout << i << " ";
    }
    return 0;
}


Output

Using iostream's cout to print
Using vector container: 11 12 14 

Non-Standard Header Files in C++ and Their Uses

Non-standard files as stated before are not the part of ISO standard of C++. They are defined by the programmer for their convenience. We have to manually install it or they may be bundled with the compiler by the vendor.

Following are some non-standard header files in C++:

Header File Description
<bits/stdc++.h>

It contains all standard libraries of the header files mentioned above. So if you include it in your code, then you need not have to 

include any other standard header files. But as it is a non-standard header file of GNU C++ library, so, if you try to compile your 

code with some compiler other than GCC it might fail; e.g. MSVC does not have this header.

<Qpushbutton> It contains a push button element of Qt GUI Library for C++.

Example:

C++




// C++ program to illustrate the use of
// non-standard bits/stdc++.h header file in C++
#include <bits/stdc++.h>
using namespace std;
 
// Driver Code
int main()
{
 
    char s1[20] = "12345";
    char s2[10] = "Geeks";
    char s3[10] = "ForGeeks";
    long int res;
 
    // All the below function are mentioned in bits/stdc++.h
    // library Find the value of 9^3
    res = pow(9, 3);
    cout << "Using bits/stdc++.h, "
            "The value is: "
         << res << "\n";
 
    // Convert a string to long long int
    long int a = atol(s1);
    cout << "Using bits/stdc++.h, the string";
    cout << " to long int: " << a << "\n";
 
    // Copy the string s3 into s2 using
    strcpy(s2, s3);
    cout << "Using bits/stdc++.h, the strings"
            " s2 and s3: "
         << s2 << s3 << "\n";
 
    return 0;
}
 
// Code submitted by Susobhan Akhuli


Output

Using bits/stdc++.h, The value is: 729
Using bits/stdc++.h, the string to long int: 12345
Using bits/stdc++.h, the strings s2 and s3: ForGeeksForGeeks

Create your own Header File in C and C++

Instead of writing a large and complex code, we can create our own header files and include them in our program to use whenever we want. It enhances code functionality and readability. Below are the steps to create our own header file:

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

C




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


C++




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


Step 2: Include your header file with “#include” in your C/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;
 
// Driver Code
int main()
{
 
    // Given two numbers
    int a = 13, b = 22;
 
    // Function declared in header
    // file to find the sum
    printf("Sum is: %d", sumoftwonumbers(a, b));
}


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;
 
// Driver Code
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

Output

The output of the above program.

Including Multiple Header Files

You can use various header files in a program. When a header file is included twice within a program, the compiler processes the contents of that header file twice. This leads to an error in the program. To eliminate this error, conditional preprocessor directives are used.

Syntax:  

#ifndef HEADER_FILE_NAME
#define HEADER_FILE_NAME

the entire header file

#endif

This construct is called wrapper “#ifndef”. When the header is included again, the conditional will become false, because HEADER_FILE_NAME is defined. The preprocessor will skip over the entire file contents, and the compiler will not see it twice.

Sometimes it’s essential to include several diverse header files based on the requirements of the program. For this, multiple conditionals are used.

Syntax:  

#if SYSTEM_ONE
        #include "system1.h"
#elif SYSTEM_TWO
        #include "system2.h"
#elif SYSTEM_THREE
        ....
#endif

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 26 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials