Skip to content
Related Articles

Related Articles

Header files in C/C++ and its uses

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 05 Jul, 2022
View Discussion
Improve Article
Save Article

C language has numerous libraries that include predefined functions to make programming easier. In C language, header files contain the set of predefined standard library functions. You request to use a header file in your program by including it with the C preprocessing directive “#include”. All the header file have a ‘.h’ an extension. By including a header file, we can use its contents in our program.
C++ 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 but in C, all the header files must necessarily end with the “.h” extension. 
A header file contains: 
 

  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 for instructing compiler that these files need to be processed before compilation. 
In C program should necessarily contain the header file which stands for standard input and output used to take input with the help of scanf() and printf() function respectively. 
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. 
There are of 2 types of header file: 
 

  1. Pre-existing header files: Files which are already available in C/C++ compiler we just need to import them.
  2. User-defined header files: These files are defined by the user and can be imported using “#include”.

Syntax: 
 

#include <filename.h>
or
#include "filename.h"

We can include header files in our program by using one of the above two syntax whether it is 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 type and function definitions.
Note: We can’t include the same header file twice in any program.
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:
 

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

CPP




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

  1. Include your header file with “#include” in your C/C++ program as shown below: 
     

CPP




// 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;
}

Below is the output of the above program: 
 

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 contents of the file 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

Standard Header Files And Their Uses: 
 

  1. #include<stdio.h>: It is used to perform input and output operations using functions scanf() and printf().
  2. #include<iostream>: It is used as a stream of Input and Output using cin and cout.
  3. #include<string.h>: It is used to perform various functionalities related to string manipulation like strlen(), strcmp(), strcpy(), size(), etc.
  4. #include<math.h>: It is used to perform mathematical operations like sqrt(), log2(), pow(), etc.
  5. #include<iomanip.h>: It is used to access set() and setprecision() function to limit the decimal places in variables.
  6. #include<signal.h>: It is used to perform signal handling functions like signal() and raise().
  7. #include<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.
  8. #include<errno.h>: It is used to perform error handling operations like errno(), strerror(), perror(), etc.
  9. #include<fstream.h>: 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.
  10. #include<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.
  11. #include<float.h>: It contains a set of various platform-dependent constants related to floating point values. These constants are proposed by ANSI C. They allow making programs more portable. Some examples of constants included in this header file are- e(exponent), b(base/radix), etc.
  12. #include<limits.h>: It determines 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.
  13. #include<assert.h>: It contains information for adding diagnostics that aid program debugging.
  14. #include<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.
  15. #include<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.
  16. #include<setjmp.h>: It contains function prototypes for functions that allow bypassing of the usual function call and return sequence.
  17. #include<stddef.h>: It contains common type definitions used by C for performing calculations.

Non-Standard Header File And its Uses:

  • #include<bits/stdc++.h>: It contains all standard library 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 do not have this header. (See this article for more reference)

Example of Standard Header Files:
Below is the program to illustrate the use of header file in C and C++:
 

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;
}

C++




// C++ program to illustrate the use of
// header file in C++
#include <iostream>
using namespace std;
#include <math.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);
    cout<<"Using math.h, "
           "The value is: "<<res<<"\n";
 
    // In C++, we need not have to include stdlib.h library
    // to convert a string to long long int
    // But for the same output as C, I add stdlib.h header file
    // In C++, iostream library do that thing
    long int a = atol(s1);
    cout<<"Using stdlib.h, the string";
    cout<<" to long int: "<<a<<"\n";
 
    // Copy the string s3 into s2 using
    // using a function in string.h library
    strcpy(s2, s3);
    cout<<"Using string.h, the strings"
           " s2 and s3: "<<s2<<s3<<"\n";
 
    return 0;
}
 
//Code submitted by Susobhan Akhuli

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 
 

Example of Non-Standard Header File <bits/stdc++.h>:

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!