C++ Comments

Comments are the chunks of lines ignored by the compiler while compiling the code.  Comments are useful for understanding and explanation of the code. C++ allows single line as well as multiple line comments.

Single Line Comments:

If any line is pre-appended by double backslashes (//) then the line gets commented and compiler ignores such lines. For example look at the following code. We can also place single line comments at the end of statement. Look at the code below.

//This code has been written by tutorialsart.com
#include <iostream>;
using namespace std;
int main()
{
cout <<"Hello World"; // This line will print Hello Word
return 0;
}

In the above code The line number is ignored completely by the compiler. But The line number five will be partially processed by the compiler. All the words after the semicolon will be ignored. The output of the above code after compilation is:

Multiple Line Comments:

If we need to comment multiple lines in the code then the lines are enclosed by  /* and */.

Look at the following code

//This code has been written by tutorialsart.com
#include <iostream>;
using namespace std;
int main()
{
cout <<"Hello World"; // This line will print Hello Word
/* These lines are comments.
These lines will be ignored by compiler.
This is example of multiple line comments.*/
return 0;
}

The output of the above code after compilation is:

C-language Style Comments:

If we use c style comments then we could have advantage in removing the comments.

The single line C style comments are written as follows

/*cout<<"This is a comment line"; /**/

and the multiple line comments are written as follows

/*
cout<<"This is a comment line1";
cout<<"This is a comment line2";
/**/

When we have to comment these lines then we have to remove only the first /*.

Un-commenting single line:

cout<<"This is a comment line"; /**/

Un-commenting multiple lines:

cout<<"This is a comment line1";
cout<<"This is a comment line2";