Tutorial Study Image

C++ Comments

August 3, 2022, Learn eTutorial

In this tutorial let us learn about C++ comments, why we use them, and how to use them with examples.

C++ comments are suggestions that a programmer can add to their code to make it easier to read and understand. C++ compilers will completely ignore them. Comments can be used to explain and improve the readability of C++ code. It can also be used to prevent execution when experimenting with different codes.

Comments shouldn't be used as a stand-in for a way to explain English-language code that isn't written well. We should always write code that is well-structured and self-explanatory. Then, use comments.

Why are the comments very important in programming?

Comments are text notes that are inserted into the program to explain the source code. They are used in a programming language to document the program and remind programmers of the tricky things they just did with the code. They also aid in the understanding and maintenance of code for future generations.

There are mainly  two methods for adding comments to code:

  1. // - Single Line Comments
  2. /* */ - Multi-line Comments

What are Single Line Comments in C++?

The //(two slashes) characters, followed by any character sequence. This type of comment is terminated by a new line that is not immediately followed by a backslash. As a result, it is mainly  referred to as a "single-line comment."

// comment

Any line in C++ that begins with // is a comment.

For example,

// declaring a variable
int a;

// here we are initializing the variable 'a' with the value 5
a = 5;

We've used two single-line comments here:

  •    // declaring a variable
  •   // here we are initializing the variable 'a' with the value 5

Comment at the End of the Code Line

You can also include a comment at the end of a line of code. However, it is generally preferable to place the comment before the line of code. This example given below applies to both C and C++ because the commenting style is the same in both languages.

We can also use a single-line comment, such as :

int a; // declaring a variable

What are Multi-Line Comments in C++?

Any text can be represented as /* any text */. Begin with a forward slash and an asterisk (/*) and end with a forward slash and an asterisk (*/).

It's used to indicate a multi-line comment. It is possible to apply the comment to more than one line. It is known as a C-Style comment because it was first used in C programming.

/*Comment starts
Comment ends*/


Any line between /* and */ in C++ is also a comment.

For an example

/* C++ program  in order to illustrate
use of
multi-line comment */
#include <iostream>
int main()
 /* Multi-line  will Welcome user comment
 written to demonstrate comments
 in C++ */
 std::cout << "Welcome to learn eTutorials";
 return 0;

// This code is contributed by lasepra123


Welcome to learn eTutorials

How to use comments for Debugging?

Comments can also be used to disable code so that it is not executed. 

For example,

#include <iostream>
using namespace std;
int main() {
   cout << "some code";
   cout << ''error code;
   cout << "some other code";

   return 0;

If we get an error while running the program, rather than removing the error-prone code, we can use comments to prevent it from being executed; this can be a useful debugging tool.

#include <iostream>
using namespace std;
int main() {
   cout << "some code";
   // cout << ''error code;
   cout << "some other code";

   return 0;

Comments can be used for a variety of purposes, including

  1. For planning as well as for reviewing: We can write the pseudocode that we planned before writing the source code in the comments. Pseudocode is a language that combines natural language and high-level programming. This makes it easier to review the source code because pseudocode is more understandable than the program.
  2. Description of the code: The programmer uses code descriptions to help others to understand his or her intent. It contains the code's summary.
  3. For the algorithmic explanation: Comments are used to explain the methodology. Diagrams and formal mathematical proofs are examples of such explanations. This could be interpreted as an explanation of the code rather than a clarification of its intent. For example, a programmer may include a comment explaining why an insertion sort was chosen over a quicksort, as the former is, in theory.
  4. For resource inclusion: Logos, diagrams, and flowcharts made up of ASCII art constructions can be inserted into source code formatted as a comment. Furthermore, copyright notices can be embedded as comments within the source code.
  5. Metadata: The program's metadata is also included in the comments. This metadata is useful for software maintenance. The metadata includes the name of the original version's creator, the current maintainer of the program, the date the first version was created, the names of the people who have edited the program files thus far, and so on.
  6. Debugging: A common method of debugging is brute force. In this approach, print statements are inserted throughout the program to print the intermediate values in the hope that some of the printed values will assist in identifying the errors. We comment on those print statements after debugging. As a result, comments are also used for debugging.
  7. Automatic documentation generation: Documentation and metadata are sometimes stored in comments by programming tools. These may also include insert positions for automatic header file inclusion, commands in order to change the file's syntax highlighting mode, or the file's revision number. These functional control comments are also known as annotations. One strategy for streamlining the documentation process and improving the likelihood that it will be updated to reflect changes in the code is to keep documentation within source code comments.
  8. Stress relief: Stress relief can be achieved by commenting on development tools, competitors, employers, working conditions, or the quality of the code itself. This phenomenon is easily visible using online resources that track profanity in source code.