Comments in C++ Programming Language With Examples

In this tutorial, we will learn what C++ comments are, why we use them, and how to use them with the help of examples given in this article.

Comments in C++ are hints that a programmer can add to make their code easier to read and understand. C++ compilers completely ignore them.

What is Comments in C++?

A comment is a text that the compiler doesn’t look at, but that is useful for programmers.

Comments are normally used to mark up code for future reference. The compiler sees them as empty space.

Comments in C++
Comments in C++

What are the 2 types of comments in C++?

There are two styles of comments in C++:

  • // – Single Line Comments
  • /* */ – Multi-line Comments

Why do we use comments in C++?

Comments can be used to explain and make C++ source code easier to read.

It can also stop code from running when testing different codes. Comments can have one line or more than one line.

How do you comment well in C++?

If a comment starts more than one paragraph long, it’s better to use a different way of writing it.

The other way to write comments in C++ is to start the comment with /* and end it with */.

You can put a one-line comment inside a multi-line comment.

How to Write C++ Comments

You can write C++ comment in one of the following ways: The /* (slash, asterisk) characters, followed by any sequence of characters (including newlines), and then the */ characters.

Single Line Comments

In C++, a comment is any line that starts with //.

// declaring a variable
int x;

// initializing the variable 'x' with the value 26
x = 26;

Here, we’ve used two single-line comments:

  • // declaring a variable
  • // initializing the variable ‘x’ with the value 26

We can also use single-line comments like this:

int z;    // declaring a variable

Multi-line comments

In any view of a C++ editor, you can comment out one or more lines of code.

When commenting on one or more lines of code, the characters // are added to the beginning of each line.

You can also use the characters /* */ to block comments on more than one line of code.

For Example:

declaring a variable

to store salary to employees

int salary = 7500;

You can use this syntax to write both single-line comments and multi-line comments.

You can also disable code with comments to stop it from running.

For Example:

#include <iostream>

using namespace std;

int main() {

   cout << "some code";

   cout << ''error code;

   cout << "some other code";

   return 0;


If we get an error when running the program, we don’t have to eliminate the code that causes the error.

Instead, we can use comments to stop it from being run. This can be a useful tool for debugging.

For Example:

#include <iostream>
using namespace std;
int main()
   cout << "some code";
   // cout << ''error code;
   cout << "some other code";

   return 0;

Why Use Comments

If we add comments to our code, it will be easier for us to understand it in the future.

Also, your fellow developers will be able to understand the code better.


This article discusses the C++ comments. It also tackles the types of comments, use of comments, and how many types of comments are there in C++, single-line comments, and multi-line comments.

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials which could help you a lot.

Leave a Comment