Comments are statements generally which are not executed by the compiler. It is written for the user to understand the purpose of the program or the source code written by the programmers. Also written to provide information about the variables, methods, class.
There are 2 types of comments in C++:
- Single Line Comments
- Multi Line Comments
Source: GeeksForGeeks
A single line comment starts with "//" i.e., a double slash.
#include <iostream>
using namespace std;
int main()
{
int j; //j is declared.
cin>>j; // reads input for j from user.
cout<<j; //displays the value of j
}
5
5
A multi line comment is used in C++ when there are many lines of code or a huge explaination or a huge question of a program. It starts and ends like "/*....*/" i.e., a slash and a asterisk.
/* This program is used to demonstrate the use of multi line comment.*/
#include <iostream>
using namespace std;
int main()
{
int j;
cin>>j;
cout<<j;
/* j is declared, then input of j is read and then it's value is displayed.*/
}
10
10