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.
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:
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:
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.
There are two categories of comments in C++:
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.
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.
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.
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 can also be written within or alongside statements to explain specific parts of the code.
The following example demonstrates how comments can be written within or alongside statements.
Output:
15
Comments are widely used for documentation purposes, especially in large programs, to describe the purpose of functions, classes, and logic.
The following example demonstrates how comments are used for documentation.
Output:
Simple Interest = 100
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India