Header files provide definitions of Functions and Variables that can be imported or utilized in any C++ program using the #include
command in the preprocessor. The extension .h
denotes a header file that contains C++ function declarations and macro definitions.
A header file provides the following details:
- Function definitions
- Data type definitions
- Macros
It provides the functionalities above by using the preprocessor directive #include
to import them into the application. These preprocessor directives tell the compiler that these files need to be processed before they can be compiled.
If we use clrscr()
in a C++ program, we must include the conio.h
header file since the definition of clrscr()
(for clearing the screen) is defined in the conio.h
header file.
The header file in a C++ program represents the input and output streams, which are used to take input with the help of cin
and cout
respectively.
Types of Header Files in C++
A header file can be one of two types:
- System-Defined Header Files: We only need to import files that are already present in the C++ compiler.
- User-Defined Header Files: These files are user-defined and can be imported using the
#include
statement.
Syntax
#include <iostream.h>
We can use one of the two syntaxes above to include header files in our application, whether they are pre-defined or user-defined header files. The #include
preprocessor tells the compiler that the header file must be processed before compilation and that it contains all of the required data type and function definitions.
A C++ Program Example that will demonstrate the need for header files in C++
// Header Files example #include <iostream> // Include the iostream header file for input/output operations using namespace std; int main() { // Output a message to the console using cout cout << "I'm a message, and I need the header file \"iostream\" to display on screen"; return 0; }
In the above program, the message in the double quotes will be displayed using the output function cout
. However, we didn’t define cout
in the program, so how can that function display the message? The answer is because it is already defined in the header file iostream
.
How to Use Header Files in a Program
The preprocessor directive #include
is used to include both user and system header files. It comes in two varieties:
Syntax
#include <filename.h>
This is the syntax for system header files. It checks in a standard set of system directives for a file named file.
#include "filename.h"
This is the syntax for our own program’s header files. It looks in the directive holding the current filename called file.
Note:
The use of angle brackets <>
instructs the compiler to look for the given file in the compiler’s include directory. The usage of double quotes ""
around the filename tells the compiler to look for the supplied file in the current directory.
Standard Header Files In C++ and Their Uses
Below listed are some of the most commonly used header files in C++.
- #include<iostream>: It works as a stream for the Input and Output of C++ Statements using
cin
andcout
. - #include<string.h>: It is used to execute string manipulation functions such as
strlen()
,strcmp()
,strcpy()
,size()
, and many more. - #include<math.h>:
Sqrt()
,log2()
,pow()
, and other mathematical operations are performed with it. - #include<iomanip.h>: It’s used to get to the
set()
andsetprecision()
functions, which limit the number of decimal places in variables. - #include<signal.h>: Signal handling operations like
signal()
and raise is performed using it()
. - #include<fstream.h>: It is used to regulate the data that is read from and written to a file as an input and output.
- #include<time.h>: It’s needed to perform
date()
andtime()
operations likesetdate()
.
Reserved Words or Keywords in C++
A reserved word is a term/name for a variable, function, or label that cannot be used as an identifier – it is “reserved from usage.” A reserved word may or may not have any meaning, according to this syntactic definition.
In C++, there are 95 reserved words. 30 of the reserved words are new to the C++ programming language since they were not in the C language.
There are 11 reserved words in C++ that aren’t necessary when using the standard ASCII character set. Still, they’ve been included to give more legible replacements for a number of the C++ operators and make programming with character sets that don’t have the characters required by C++ easier.
You can download a PDF list of Reserved words used in C++.
1 comment
It’s very helpful. Thanks for sharing ; )