C++ Comments

Comments are used to describe the code. These lines are not displayed in the website. It offers an option of leaving notes for yourself or others. The comment simplifies the production of more readable code. Like C there are two types of comments in C++:

  • Single-line Comment
  • Multi-line Comment


Single-line Comment

A single-line comment is represented by //. It is used for commenting a single line.

Example

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. // This is a single line comment
  6. cout << "Hello, World!";
  7. return 0;
  8. }

Output:

Hello, World!


Multi-line Comment

Multi-line comments open with /* and close with */.

Example

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. /* This is a
    multi-line comment */
  6. cout << "Hello, World!";
  7. return 0;
  8. }

Output:

Hello, World!