What is comments in C++?

Comments in C++ are explanatory text which is not compiled by compiler although we use them inside our main program. Every programming language supports comment and it increases the readability of code. Comments can be any explanatory statement or some line of code which we need to skip compiling.

Method of writing comments in C++

As we have mentioned above that every programming language supports comments. But the method of writing comments is not same for every language. In C++ we can write comments in two way.

  • Single line comment
  • Multiline comment

We can also use nested comment which means a comment inside another and this also discussed here in this article.

Single line comments in C++

Single line comment is used only in one line. The text which we will write after // is the single line comment.

// declaring variable and assigning value
int x = 7, y = 12;

// determining the sum
int sum = x + y;

We can also write these two comments as like as following.

int x = 7, y = 12;    // declaring variable and assigning value

int sum = x + y;     // determining the sum

Although we can use any number of comments inside our program. But it is not recommended to use much comment inside a program. This will decrease the readability of code.

Multi-line comments in C++

Any text inside /* and */ is also comment in C++. We can use any number of line inside them to skip them from being compiled.

int x = 12, y = 21;

/* This program will add
x with y and
print the sum of x and y */

int result = x + y;

We can also comment out any valid code to skip them from being compiled by the compiler. This is actually used in debugging purpose. We can test any block of code without the influence of other code by making them comment.

#include <iostream>
using namespace std;

int main(){
    char x = 10;
    int y = 15;
    int z = 25;

    // sum will be 10 + 15 + 25 = 50

    // x = 20;
    // y = 35;
    // z = 40;

    // sum will be 20 + 35 + 40 = 95

    /* Although we have changed the value of
    all the variable here, but the value which
    we have assigned first will be taken into
    account and the program will print the sum = 50 */

    int sum = x + y + z;
    cout << sum << endl;

    return 0;
}

When you compile and run this program you will see the sum is 50. But if the code inside the comment was being executed then the sum should be 95.

comments-in-c-use-of-comments-in-c

We can also use nested comment. Nested comment means using one kind of comment inside another comment.

float a, b;
a = 4.6;
b = 3.54;

/* this program will multiply
variable a with b // second will divide a by b
comments are funny */

float mul = a * b;
float div = a / b;

Use of comments in C++

We use comments in C++ for a number of purpose. Although a programmer can use comment for any purpose but mainly a comment is used to describe the code to the readers. Here are some reasons why we use comments inside our program.

  • To describe a block of code or a single line of code
  • To give the idea about the purpose of the program
  • Comments are largely used in debugging purpose
  • They provides additional information of the program
  • To understand the code in future which will make easy to edit our program
  • We can explain the cause of doing something rather than other by a comment.

Previous pageInput output in C++

Next page:  Variables in C++