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++ CommentsC++ 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 CommentsA 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.
SyntaxThe following syntax shows how to use a single-line comment in C++:
// Text to be commentedExample
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.
SyntaxThe 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 << "Tutorials Point"; return 0; }
Output
Hello World! Tutorials PointComments 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.
ExampleThe 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 commentExample
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 commentNesting 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.
ExampleThe 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 CommentsComments are used for various purposes in C++. Some of the main areas of application of comments are given as follows:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4