Loading...
C++ Comments

C++ Comments

In this tutorial, we will learn about C++ comments, types of Comments, why we use them, and how to use them with the help of examples.


Comments

  • A well-documented program is a good practice as a programmer. It makes a program more readable and error finding became easier.
  • C++ comments are hints that a programmer can add to make their code easier to read and understand.
  • Comments are statements that are not executed by the compiler and interpreter.
  • Comments are One of the important part of good documentation.

Types of Comments

There are two ways to add comments to code:

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


1. Single Line Comments

  • It is used to denote a single line comment.
  • In C++, any line that starts with // is a Single-line comment.
  • It is referred to as C++ style comments as it is originally part of C++ Programming.

For example,

// declaring a variable
char a;

// initializing the variable 'a' with the value 2
a = 2;

Here, we have used two single-line comments:

  • // declaring a variable
  • // initializing the variable 'a' with the value 2

We can also use single line comment like this:

char a;    // declaring a variable

2. Multi-line comments

  • In C++, any line between /* and */ is also a comment.
  • It is used to denote Multi-line Comment.
  • It can apply comment to more than a single line.

For example,

/* declaring a variable to store Name */
int salary = Algbly;

This syntax can be used to write both single-line and multi-line comments.


Using Comments for Debugging

Comments can also be used to disable code to prevent it from being 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, instead of removing the error-prone code, we can use comments to disable it from being executed;
  • this can be a valuable debugging tool.

Let's see next example to use comments as debugging tool.

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

    return 0;
}

Pro Tip: Remember the shortcut for using comments; it can be really helpful. For most code editors, it's Ctrl + / for Windows and Cmd + / for Mac.


Why use Comments?

  • Comments can include a description of an algorithm to make code understandable.
  • If we write comments on our code, it will be easier for us to understand the code in the future.
  • Also, it will be easier for your fellow developers to understand the code.

Note: Comments shouldn't be the substitute for a way to explain poorly written code in English. We should always write well-structured and self-explanatory code. And, then use comments.

As a general rule of thumb, use comments to explain Why you did something rather than How you did something, and you are good.


Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Comments in C++.

Keep Learning : )

In the next tutorial, you'll learn about C++ If else Statement.

- Related Topics