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++:
Always make sure to follow the points below while using the header file in C++ while writing a program:
- The header files should only be included while you start writing a program even before declaring any variable.
- One #include command should be used to include one header file only.
- 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:
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.
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++.
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.