In C++, comments let you add notes and explanations to source code without making those notes part of the program’s executable statements. C++ supports single-line comments beginning with // and block comments enclosed by /* and */. This tutorial explains both forms with examples and covers important details such as inline comments, multi-line comment blocks, and why block comments should not be nested.
C++ Comments and Their Purpose in Source Code
Comments are portions of C++ source code intended for programmers rather than program execution. They are useful for explaining why code exists, documenting assumptions, describing non-obvious logic, and leaving concise notes for someone reading the program later.
C++ provides two comment forms:
- Single-line comments begin with
//and continue to the end of the current line. - Block comments begin with
/*and end with*/. They can occupy one line or span multiple lines.
Comments do not produce program output. For example, a comment placed beside a cout statement explains the code but does not change the text printed by that statement.
Single-Line Comments in C++ with //
To write a single-line comment in C++, use two forward slashes //. Everything from // to the end of that source line is treated as comment text.
C++ Single-Line Comment Syntax
// comment text
A single-line comment may appear on a line by itself or after a C++ statement.
C++ Example with Standalone and Inline // Comments
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.
The second form is commonly called an inline or end-of-line comment. Only the text after // is a comment; the C++ statement before it remains part of the program.
Multi-Line or Block Comments in C++ with /* and */
C++ block comments begin with /* and end with */. All text between these delimiters belongs to the comment, even when it spans several source lines.
C++ Multi-Line Comment Syntax
/*
comment text
can span multiple lines
*/
C++ Example with a Multi-Line Comment Block
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!";
}
The comment begins at /* and continues until the matching */. The cout statement appears after the block comment and therefore remains normal C++ code.
C++ Block Comments Can Also Fit on One Line
Despite the common name “multi-line comment,” a /* ... */ comment does not have to span multiple lines. It may be written on a single line as well.
/* This is a block comment on one line. */
The important distinction is therefore the delimiter used: // ends at the end of the line, whereas /* ... */ ends only when its closing delimiter is encountered.
Difference Between // and /* */ Comments in C++
Both forms create comments, but they terminate differently. A // comment automatically ends at the end of its line. A /* ... */ comment may continue across line boundaries until */ is reached.
| Comment form | Starts with | Ends at | Typical use |
|---|---|---|---|
| Single-line comment | // | End of the current line | Short explanations and notes beside statements |
| Block comment | /* | */ | Comments that need to cover more than one line or a bounded section of text |
Nested Block Comments Are Not Supported in C++
C++ block comments do not nest. If /* appears inside an existing block comment, it does not start an independent nested comment. The first subsequent */ closes the comment.
For example, code shaped like the following should not be used as a nested-comment technique:
/* outer comment
/* inner-looking text */
more text
*/
The */ after inner-looking text already terminates the block comment. The remaining text is then interpreted according to normal C++ source rules and may result in compilation errors.
When commenting a section that may already contain block comments, using // on each line avoids this nesting problem.
Comment Markers Inside C++ String Literals
Text that looks like a comment marker is not necessarily a comment. When // or /* ... */ appears as part of a string literal, those characters belong to the string.
#include <iostream>
using namespace std;
int main() {
cout << "https://example.com" << "\n";
cout << "/* not a comment */" << "\n";
return 0;
}
Output
https://example.com
/* not a comment */
In this example, the apparent comment markers occur inside quoted strings, so they are printed as ordinary characters.
Writing Clear Comments in C++ Programs
Useful comments generally explain information that is not obvious from the code itself. In particular, comments can document the reason for an unusual condition, an assumption made by an algorithm, a unit of measurement, or a constraint imposed by another part of a program.
- Keep comments close to the code they describe.
- Prefer explaining why a decision was made when the code already makes what it does obvious.
- Update comments when the related code changes so that the explanation does not become misleading.
- Use
//for concise notes and/* ... */when a bounded block is more appropriate. - Avoid filling a program with comments that merely repeat simple statements in words.
C++ Comments Summary
C++ supports two comment forms. A single-line comment starts with // and continues to the end of that line. A block comment begins with /* and continues until */, allowing the comment to span multiple lines. Block comments cannot be nested, and apparent comment markers inside string literals are part of the string rather than comments.
In this C++ Tutorial, we learned how to write single line and multiple line comments in C++ programs.
TutorialKart.com