Hey guys, what is up. In this particular post, we will be talking about what are header files in C++ coding. Also, we will see some examples of header files in C++.
Updated on 26/9/2021
First of all, I want to explain Header files in C++ with an example:
int a; //variable declaration
cin>>a; //taking input
cout<<a; //showing output
This is an example to print a number taking input from the user.
# is a pre-processor directive. Which executes the instructions before the compilation of the program. In this code, it will include the header files in C++ code like iostream.h and conio.h.
Include statement includes the header files in C++ code given in angle brackets <>.
What are header files in C++?
Header files are the files that we need to use in a code. These are predefined in the C++ library. There are 49 header files in the C++ library. In this iostream.h and conio.h are pre-defind header files. iostream.h is required whenever the input and output stream objects are used in a program. We use conio.h when we want to control the time of output visibility on the output screen.
void is a datatype which is a return type for function main as it does not return any value.
main: It is a function that performs a specific task. The main function is special as program execution begins from the main function. Also, program execution ends at the main function until the program is stopped forcefully. I.e. every program should have the main function. The main function is followed by parenthesis () to indicate that it does not contain any arguments.
Every function body is followed by a function body. It is enclose in braces. The function body is a set of executable statements when the function is executed.
//,/*,*\ these are comment statements. These are non-executable. As it is ignored by the compiler. Also, we use comments to make the program readable and understandable. Comments are of two types:
- Single Line comment (//): It begins with // and remains the comment in the full line.
- Multi-line comment: It starts with /*. And ends with *\. As the name suggests comment in multiple lines.
Comments are also useful for debugging a program as parts of the program can be used to check errors. While other parts can be marked as comments.
cout: cout is the object of the output stream i.e. it is the character of the iostream class.
cin: cin is the object of the input stream. It is also the character of the iostream class.
;– It is the statement terminator in C++ which is essential to terminate a program.
Input operator in C++ is the extraction operator (>>). It is used with the cin object and extracts data from the input stream to the variable on its right side. Ex: cin>>a; where a is a variable and we want to take the input from the user.
The output operator in C++ is the insertion operator (<<). Similarly, it is used with a cout object which inserts the expression on the right-hand side of the output stream. Ex: cout<<a; here we are showing the value of variable a.
When there is a need for multiple uses of cin and cout object then we use cascading. Cascading means multiple uses of input and output operators in a single statement. Ex: cin>>n>>m; Here we are taking input of n and m together.
Ex: cout<<n<<endl<<m<<endl; Here we are displaying n and m together with the help of a single cout object.
In the upcoming post, you will be getting information related to the if-else, latest technology and some amazing facts about the technology. If you want to know about what are tokens and their types in C++ then go and read this.