In 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 but in C, all the header files must necessarily end with the “.h” extension.
A header file contains:
It offer 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:
- Pre-existing header files: Files which are already available in C/C++ compiler we just need to import them.
- User-defined header files: These files are defined by the user and can be imported using “#include”.
#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:
- Write your own C/C++ code and save that file with “.h” extension. Below is the illustration of header file:
// Function to find the sum of two
// numbers passed
(a + b);
- Include your header file with “#include” in your C/C++ program as shown below:
// C++ program to find the sum of two
// numbers using function declared in
// header file
// Including header file
// Driver Code
// Given two numbers
a = 13, b = 22;
// Function declared in header
// file to find the sum
"Sum is: "
<< sumOfTwoNumbers(a, b)
- Below is the output of the above program:
Below are some inbuilt header files in C/C++:
- #include<stdio.h>: It is used to perform input and output operations using functions scanf() and printf().
- #include<iostream>: It is used as a stream of Input and Output using cin and cout.
- #include<string.h>: It is used to perform various functionalities related to string manupulation like strlen(), strcmp(), strcpy(), size(), etc.
- #include<math.h>: It is used to perform mathematical operations like sqrt(), log2(), pow(), etc.
- #include<iomanip.h>: It is used to access set() and setprecision() function to limit the decimal places in variables.
- #include<signal.h>: It is used to perform signal handling functions like signal() and raise().
- #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.
- #include<errno.h>: It is used to perform error handling operations like errno(), strerror(), perror(), etc.
- #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.
- #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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- What’s difference between header files "stdio.h" and "stdlib.h" ?
- <complex.h> header file in C with Examples
- Exception header in C++ with examples
- accumulate() and partial_sum() in C++ STL : numeric header
- numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
- Namespace in C++ | Set 3 (Accessing, creating header, nesting and aliasing)
- random header in C++ | Set 1(Generators)
- random header | Set 2 (Distributions)
- Print "Hello World" in C/C++ without using any header file
- random header in C++ | Set 3 (Distributions)
- Difference between Header file and Library
- clocale header file in C++
- isalpha() and isdigit() functions in C with cstring examples.
- Pointers in C/C++ with Examples
- strtok() and strtok_r() functions in C with examples
- memset() in C with examples
- std::mismatch() with examples in C++
- wcscpy() function in C++ with Examples
- wcscmp() function in C++ with Examples
- set_symmetric_difference in C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.