Comments in C++
Program comments are explanatory statements that you can include in the C++ code. These comments help anyone reading the source code. All programming languages allow for some form of comments.
Types of C++ Comments
C++ supports two types of comments: single-line comments and multi-line comments. All characters available inside any comment are ignored by the C++ compiler.
The types of C++ comments are explained in detail in the next sections:
1. C++ Single-line Comments
A single-line comment starts with //, extending to the end of the line. These comments can last only till the end of the line, and the next line leads to a new comment.
Syntax
The following syntax shows how to use a single-line comment in C++:
// Text to be commented
Example
In the following example, we are creating single-line comments −
#include <iostream>
using namespace std;
int main() {
// this is a single line comment
cout << "Hello world!" << endl;
// for a new line, we have to use new comment sections
cout << "This is second line.";
return 0;
}
Output
Hello world!
This is second line.
2. C++ Multi-line Comments
Multi-line comments start with /* and end with */. Any text in between these symbols is treated as a comment only.
Syntax
The following syntax shows how to use a multi-line comment in C++:
/* This is a comment */
/*
C++ comments can also
span multiple lines
*/
Example
In the following example, we are creating multi-line comments −
#include <iostream>
using namespace std;
int main() {
/* Printing hello world!*/
cout << "Hello World!" << endl;
/*
This is a multi-line comment
Printing another message
Using cout
*/
cout << "TheMAK Pro";
return 0;
}
Output
Hello World!
TheMAK Pro
Comments within Statements
We can also comment-out specific statements within a code block inside a C++ program. This is done using both types of comments.
Example
The following example explains the usage of multi-line comments within statements −
#include <iostream>
using namespace std;
int main() {
cout << "This line" /*what is this*/ << " contains a comment" << endl;
return 0;
}
Output
This line contains a comment
Example
The following example explains the usage of single-line comments within statements −
#include <iostream>
using namespace std;
int main() {
cout << "This line" // what is this
<< " contains a comment" << endl;
return 0;
}
Output
This line contains a comment
Nesting Comments
Within a /* and / comment, // characters have no special meaning. Within a // comment, / and */ have no special meaning. Thus, you can “nest” one kind of comment within the other kind.
Example
The following example explains the usage of comments within comments using nesting −
#include <iostream>
using namespace std;
int main() {
/* Comment out printing of Hello World:
cout << "Hello World"; // prints Hello World
*/
cout << "New, Hello World!";
return 0;
}
Output
New, Hello World!
Single-line or Multi-line Comments - When to Use?
Single-line comments are generally used for short lines of comments in general. This is seen in cases where we have to mention a small hint for the algorithm in the code.
Multi-line comments are generally used for longer lines of comments, where the visibility of the whole comment line is necessary. The longer the length of the comment, the more number of statements are needed by the multi-line comments.
Purpose of Comments
Comments are used for various purposes in C++. Some of the main areas of application of comments are given as follows:
- To represent a short and concise step in the program for users to understand better.
- To explain a step in a detailed way that is not expressed explicitly in the code.
- To leave different hints for users to grab in the code itself.
- To leave comments for fun or recreation.
- To temporarily disable part of the code for debugging purposes.
- To add metadata to the code for future purposes.
- To create documentation for the code, for example, in Github pages.