Updated April 4, 2023
Introdcution to C++ file header
In C++, the header file consists of definitions of several functions and variables that are used or imported in the program with the help of the pre-processor # includes syntax. Moreover, this header file contains an extension “.h” that is the source of function and other macro statements. The different header file contains the details of a specific function. Similar to stdio.h that contains standard input and output functions, several other header files also exist. In this article, more details on header files will be explained.
Syntax
Below is the syntax of the file header.
#include <filename.h>
In addition to that, another syntax, as shown below, can also be used.
#include "filename.h"
This can be used in any of the pre-defined as well as user-defined header files.
How does file header work in C++?
Before moving to the working of header files, let us see different types of header files, examples, and their uses.
There are two types of header files available in C++.
- System header files which are available with the compiler.
- User header files which the programmer writes.
The following are the standard header files and its definition.
Header file | Use |
|
Header file that consists of a set of several platform-dependent constants which are proposed by ANSI-C that are related to floating-point values. They permit the creation of programs more portable. Example: e – exponent), b – base |
|
Header file that controls several properties of the different variable types. |
|
Header file that performs input as well as output operations with the help of functions printf() and scanf(). |
|
Header file that performs date and time related functions such as date(), setdate(), getdate() etc. That is, it also helps in modifying the system date as well as getting the CPU time. |
|
The header file is used to control the data to read from a file as input and data to write into a file as output. |
|
Header file that is used as input and output stream with the help of cin and cout. |
|
Header file that is used to perform different error handling functions such as errno(), perror(), strerror(). |
|
Header file that performs different mathematical operations with the help of functions such as pow(), sqrt(), log2(). |
|
Header file that is used to perform different standard argument functions such as va_arg() and va_start(). |
|
The header file is used to grant functions such as set() and setprecision() to limit the variable’s decimal places. |
|
Header file that is used to perform string manipulation with the help of functions such as strlen(), size(), strcmp(), strcpy(). |
|
Header file that that is used to perform different signal handling functions such as raise() and signal(). |
If any functions have to be added to our C++ program, we must import the corresponding header files. On importing it, all the necessary functions will be included inside it. The header file is present at the starting of the C++ program with a preprocessor directive #include. The preprocessor “#include” directs the compiler that processing should be done for the header file before compilation and consists of all the essential functions and data type definitions.
Let us consider an example.
In order to perform a square root operation of a number, we have to import the header file <math.h> as shown in the below program.
//header files
#include <math.h>
#include <stdio.h>
int main()
{
int num=16;
sq = sqrt(num);
…...
……..
}
In this program, a number is initialized, and the square root is found using the sqrt() function available in <math.h> header file
Examples of C++ file header
In order to understand more about header files, let us work on a few more c++ programs.
Example #1
C++ program to perform a mathematical function using the <math.h> header file.
Code:
//header files
#include <math.h>
#include <stdio.h>
int main()
{
long int exampl;
// find power of 16 to 4
exampl = pow(16, 4);
printf("Power of 16 to 4 is: %ld\n", exampl);
return 0;
}
Output:
In this program, the power of 16 to 4 is found using the function pow(). In order to get the output of this mathematical function, the header file <math.h> is used. In addition to that, to handle standard input and output, <stdio.h> header file is also used. On executing the code, the power of 16 to 4 will be displayed.
Example #2
C++ program to perform string to long int conversion using the <stdlib.h> header file.
Code:
//header files
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[15] = "2443";
// String to long int conversion
long int cnv = atol(a);
printf("converted string is: %ld\n", cnv);
return 0;
}
Output:
In this program, the string too long int conversion is done using the function atol(). In order to get the output of this function, the header file <stdlib.h> is used. In addition to that, to handle standard input and output, <stdio.h> header file is also used. On executing the code, a converted string will be displayed.
Example #3
C++ program to copy a string to another string using the <string.h> header file.
Code:
//header files
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[15] = "Happy";
char b[15] = "Moments";
printf("strings a and b are : %s %s\n",a,b);
// Copy the b string into a
strcpy(a, b);
printf("strings a and b are : %s %s\n",a,b);
return 0;
}
Output:
In this program, a string is copied into another using the function strcpy(). In order to get the output of this function, the header file <string.h> is used. In addition to that, to handle standard input and output, <stdio.h> header file is also used. On executing the code, string a and b before and after copying will be displayed.
Conclusion
In C++, header files that contain functions and variables that are used or imported in the program with the help of the pre-processor # include syntax. In this article, different aspects such as syntax, working, and examples of header files are explained in detail.
Recommended Articles
This is a guide to the C++ file header. Here we discuss How does file header work in C++ and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –