C++ - SPLessons

C++ Comments

Chapter 30

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Comments

C++ Comments

Comments

shape Description

CPP Comments are the statements used in the C++ code to make the user to understand what is happening in the source code given to the compiler without reading the actual code. CPP Comments makes the new user to learn the language easily.

Eg:#include <iostream> // header file

The above code says that the #include is a header file.

  • The comments starts with the symbol //. They can be represented with a different color in the compiler.
  • Once if //  is placed in the begin of a statement, then the entire is considered as a comment and will not be involved in the source code.
  • CPP Comments helps in finding a particular code.
  • Comments can be of single line or multi-lines.

Comments are of two types. They are:

Single line comments

The single line comments helps in commenting shortly at the end of a code and can be represented with //.

int main()      //main function is of integer type
cout<<"splessons"<<endl;   // cout is console output

Multi line comments

The multi line comments can be used when there is lot of information to be inserted in source code for ease of programming. Multi-line comments can be even in 1 or more lines. They can be represented with /* ...*/

/* Welcome to splessons, you are reading C++ comments*/
int main()
{
   cout<<"splessons"<<endl;
}

Or

/* Welcome to splessons,
you are reading C++ comments
This is multi-line comments*/
int main()
{
   cout<<"splessons" <<endl;
}

shape More Info

Sometimes, there comes a need to remove the code and again place it while testing a particular project. Remembering every line becomes hard here. So, to overcome this difficulty, comments are introduced.

CPP Comments can hide the code without taking a particular statement into consideration. This can be done by just placing the comment line before the code.

cout<<"splessons";         // non-commented output
//cout<<"splessons";       //commented output

Commenting of code can be applied on multiple lines.

Summary

shape Key Points

  • CPP Comments explains program in brief.
  • Single-line comments and multi-line comments are the types.
  • //  denotes single line and /*  */ denotes multi-line comments.

shape Programming
Tips

Never use multi-line CPP Comments in nested form because it causes confusion when opening and closing of nested comment.