Header Files in C++

In the C++ programming language, header files are text files that contain declarations and macros. By using the #include command, these declarations and macros can be used when writing a C++ program. In this article, I’ll give you an introduction to header files in C++.

What are Header Files?

Let’s understand the meaning of a header file in C++ by looking at the example of iostream and my header.h.

The iostream is used for the declaration of input and output operations i.e, cin, cout. And the myheader.h is used for the declaration of self-defined functions and classes in C++. The image below describes the use of both these header files in C++:

header files

Always make sure to follow the points below while using the header file in C++ while writing a program:

  1. The header files should only be included while you start writing a program even before declaring any variable.
  2. One #include command should be used to include one header file only.
  3. The name of the header file should be enclosed by using the angle brackets or double-quotes.

Standard Header Files in C++

Now let’s have a look at the standard header files provided by the C++ standard library:

algorithmiosmapstack
bitsetiosfwdmemorystdexcept
complexiostreamnewstreambuf
dequeueistreamnumericstring
exceptioniteratorostreamtypeinfo
fstreamlimitsqueueutility
functionallistsetvararray
iomaniplocalesstreamvector

The identifiers in the header file of the C programming language are also used while writing programs using C++. Sometimes this causes name conflicts while writing larger programs. So, for this reason, each header file of the C language is used in C++ by the second name i.e, cname which is used to declare the same header file of C using the standard namespace.

Also, Read – 100+ Machine Learning Projects Solved and Explained.

The string.h or cstring which are the header files of C must be included in C++ programs that use the functions to manipulate strings. Let’s have a look at an example below by using the string header file in C++.

Example:

Here I will write a sample program by using the C++ programming language to count the length of the name:

What is your name? :Aman Kharwal
----------------------------------------
Hello Aman Kharwal
 Your name is 12 characters long.       
----------------------------------------

I hope you liked this article on an introduction to the header file in C++ programming language. Feel free to ask your valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1500

Leave a Reply