C++ Comments

Program comments are explanatory statements. You can include comments in your C++ code, which improves the readability of the source code. All programming languages allow some form of comments.

C++ supports single-line comments and multi-line comments. All characters in comments are ignored by the C++ compiler.

C++ comments generally have two types:

  • //- Usually used for single-line comments.

  • /* ... */- Usually used for multi-line comments.

Comments start with//It starts at and continues until the end of the line. For example:

Example

#include <iostream> using namespace std; int main() { // This is a comment cout << "Hello World!"; return 0; }

It can also be placed after a statement:

Example

#include <iostream> using namespace std; int main() { cout << "Hello World!"; // Output Hello World! return 0; }

When the above code is compiled, the compiler will ignore// This is a commentand// Output Hello World!, and finally the following result will be produced:

Hello World!

C++ comments end with/*starts with*/termination. For example:

#include <iostream> using namespace std; int main() { /*This is a comment*/ /*C++ comments can also * span multiple lines*/ cout << "Hello World!"; return 0; }

In/*and*/Inside a comment,//The character has no special meaning. In//Within a comment,/*and*/the characters, as they have no special meaning. Therefore, you can nest one type of comment within another. For example:

/*A comment for outputting Hello World cout << "Hello World"; // Output Hello World*/
other extensions