C++ Comment

In this tutorial, we will learn about Comment in C++. Why we use them, and how we use them, we will study in this tutorial with the help of many examples.

C++ Comment are used to hint that a programmer can add to make their code easier to read and understand. The comments help anyone reading the source code. Comment are the statement that are completely ignored by the compiler. There are two types of comments in C++.

  • Single Line comment  //
  • Multi Line comment   /* */

Single Line Comment

In C++, any line that starts with // (double slash) is a single line comment. For example,

#include "iostream"
using namespace std;
void main()
{
 // declaring a variable
 int c;
 
 // initializing the variable 'c' with the value 2
 c = 2;

 // print the variable 'c'
  cout << c ;
}

Here, we have used three single-line comments:

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

In another way we use single line comment like this:

#include "iostream"
using namespace std;
void main()
{
 int c;  // declaring a variable
 
 c = 2;  // initializing the variable 'c' with the value 2

 cout << c;  // print the variable 'c'
}

Multi Line Comment

In C++, Multi Line Comment is used to comment multiple Lines of code. Any Line between slash and asterisk (/∗ ..... ∗/) is a Multi Line Comment. For example,

#include "iostream"
using namespace std;
void main()
{
 /* declare a variable to
 store age of student */
 
 int age = 23 ;
 c = 2;
}

you know, multiple line comment is also used if you need single line comment. For example,

#include "iostream"
using namespace std;
void main()
{
 /* declare a variable */
 int age ;
}

Why use Comments

Commenting involves adding Human Readable Information inside of programs detailing the particular Code does. Proper usage of commenting could make code maintenance easier, as well as helping make finding bugs faster.

If we write comment on our computer program, it will be far easier for us to understand the computer program in the future. Also, it will be far easier for your fellow developers to understand the computer program.

Post a Comment

0 Comments