Learn with Yasir

Share Your Feedback

C++ Comments Tutorial | Single-Line and Multi-Line Comments


Learn how to use comments in C++ with examples. Understand single-line and multi-line comments, why comments are important, and how they improve readability in C++ programs.

1. What are Comments?

Comments are notes written inside a program to explain the code.

Comments are ignored by the compiler, so they do not affect the execution of the program.

Why do we use comments?

Comments help us to:

  • Explain the purpose of code
  • Make programs easier to understand
  • Remember what a section of code does
  • Make code easier to maintain

2. Types of Comments in C++

There are two common types of comments:

  1. Single-line comments
  2. Multi-line comments

3. Single-Line Comments

A single-line comment is used to write a comment on one line.

It starts with:

//

Everything after // on that line is treated as a comment.

Example 1

#include <iostream>
using namespace std;

int main() {
    // This is a single-line comment

    cout << "Hello World";

    return 0;
}

Output

Hello World

The compiler ignores:

// This is a single-line comment

Example 2: Comment After Code

int age = 20;  // Store student's age

Here:

  • int age = 20; → actual code
  • // Store student's age → comment

4. Multi-Line Comments

A multi-line comment is used when the comment covers more than one line.

It starts with:

/*

and ends with:

*/

Example

#include <iostream>
using namespace std;

int main() {

    /*
       This program displays
       a simple message
       on the screen.
    */

    cout << "Hello World";

    return 0;
}

Output

Hello World

Everything between:

/*

and:

*/

is ignored by the compiler.


5. Difference Between Single-Line and Multi-Line Comments

Single-Line Comment Multi-Line Comment
Used for one line Used for multiple lines
Starts with // Starts with /*
Ends at the end of the line Ends with */
Example: // My comment Example: /* My comment */

6. Complete Example

#include <iostream>
using namespace std;

int main() {

    // Declare two numbers
    int a = 10;
    int b = 20;

    /*
       Add the two numbers
       and display the result.
    */
    int sum = a + b;

    cout << sum;

    return 0;
}

Output

30

⭐ Quick Revision

Single-Line Comment

// This is a comment

Multi-Line Comment

/*
   This is a
   multi-line comment
*/

Remember

Comments are written for programmers, not for the compiler.

They help make C++ programs clear, readable, and easy to understand.