Comments are added in C++ program for documentation purpose. Comments are explanatory statements that we can in our code to make it easy to understand for anyone reading this source code. We can add comments anywhere and any number of times in our programs. Adding comments in your code is highly recommended and is considered as a good programming practice. Comment are ignored by C++ compilers.
C++ programming language supports two types of comments- Single line comments.
- Multi line comments(inherited from C).
Single Line Comments in C++
- Single line comment starts with “//” symbol and till end of the line.
- Everything on the line after // is ignored by compiler.
// My first C++ Program cout << "Hello_World";
cout << "Hello_World"; // My first C++ Program
Multiple Line Comments in C++
- Multi line comments in C++ start with /* and end with */. Everything between /* and */ is ignored by compiler.
- Nesting of multi-line comments is not allowed.
- Inside multi line comment // characters have no special meaning.
/* This is my first C++ program to print Hello World string on screen */ cout << "Hello World";
cout << "Hello World"; /* My first C++ Program */
Summary
- Comments are always ignored by C++ compiler.
- Comments are used for documentation and explanatory purpose.
- We can add comments anywhere and any number of times in our programs.
- In C++, a comment can be single line or multi-line.
- Adding comments in program is a good programming practice.