In this C++ tutorial, you will learn how to write single line comments and multiple line comments in the C++ programs.
C++ Comments
Comments in a C++ program are not compiled and are ignored during execution. Comments are used to increase the readability of the program.
There are two types of comments in C++ based on number of lines the comment is. They are:
- Single Line Comments
- Multiple Line Comments
Single Line C++ Comments
To write single line comments in C++ program, use double forward slash //
. Anything after //
in that line is considered a comment.
Example
In the following example, we shall write a C++ program, with single line comments.
C++ Program
#include <iostream>
using namespace std;
int main() {
//this is a comment
cout << "Hello World!"; //another comment
}
In the above program, line-5 is a comment. It started with //
and then there is some text. Whole line is a comment.
In line-6, we have a statement to print to standard output. After the statement, in the same line, we have written a comment //another comment
.
These are single line comments in C++.
Multiple Line C++ Comments
You can also write comments that span over two or more lines.
To write multiple line comments, start your comment with /*
and end with */
.
Example
Following is an example program, where we have a multiple line comment enclosed between /*
and */
.
C++ Program
#include <iostream>
using namespace std;
int main() {
/*
This is a
multiple line
comment
*/
cout << "Hello World!";
}
These are multiple line comments.
Conclusion
In this C++ Tutorial, we learned how to write single line and multiple line comments in C++ programs.