C++ Comments

Last Updated : 5 May 2026

Comments are an important part of C++ programming that help improve code readability and understanding. They allow programmers to explain the logic and describe the functionality of the code so that it becomes easier for others to understand and maintain.

In this chapter, you will learn about comments in C++, how to write them, their types, and how they are used for documentation.

What are Comments in C++?

In C++, comments are non-executable statements used to explain the code. They are ignored by the compiler and do not affect the execution of the program. Comments are mainly used to describe variables, functions, or specific parts of the code to make it more understandable.

In C++, there are mainly two types of comments:

  • Single-line comment
  • Multi-line comment

Creating / Writing Comments in C++

Comments can be written using special symbols (// for single-line comment, and /* … */ for multiline comment). The compiler ignores everything written as a comment.

Here is syntax to write comment in your code:

Example of C++ Comments

The following example demonstrates the use of comments in C++.

Output:

Hello! Welcome to the TpointTech World

Explanation:

In this example, we have taken several comments to explain the program easily. Line started by (//) is a comment line that is not compiled by the compiler.

Types of C++ Comments

There are two categories of comments in C++:

  1. Single Line comment
  2. Multi-Line comment

Single Line Comment

A single-line comment in C++ is written using a double forward slash (//). It has no effect on program execution, as the compiler ignores everything written after // on that line. Single-line comments are commonly used to explain code, highlight specific parts, or temporarily disable lines during testing or debugging.

Example

Let us examine a C++ example of a Single Line comment.

Output:

Sum = 30

Explanation:

This program takes two numbers as input from the user and stores them in variables. It then calculates their sum and displays the result. Single-line comments are used to explain each step of the code, making it easier to understand.

Multiple Line Comment

Multi-line comments are used to describe multiple lines of code or provide detailed explanations. They begin with /* and end with */. The compiler ignores everything written between these symbols, so they do not affect program execution. Multi-line comments are useful for documentation, explaining complex logic, or temporarily disabling blocks of code during debugging.

Example

The following example demonstrates the use of multi-line comments.

Output:

Area = 20

Explanation:

In this example, the program calculates the area of a rectangle using the length and width entered by the user. It takes input values, computes the area using the formula, and displays the result. Multi-line comments explain the overall logic, while single-line comments make each step easier to understand.

Comments within the Statement

Comments can also be written within or alongside statements to explain specific parts of the code.

Example

The following example demonstrates how comments can be written within or alongside statements.

Output:

15

Documentation Using Comments

Comments are widely used for documentation purposes, especially in large programs, to describe the purpose of functions, classes, and logic.

Example

The following example demonstrates how comments are used for documentation.

Output:

Simple Interest = 100

Next TopicC++ Functions