Comments are explanatory notes written inside a C++ program. They are mainly used to make source code easier to understand and maintain. Comments are ignored by the compiler when it processes the program.
A comment is text written in source code to explain what the code does. It is not executed as part of the program.
// This is a comment
The compiler ignores the comment as program instructions.
Comments can make programs easier to read and understand. They are especially useful when a program contains complex logic.
C++ provides two commonly used comment styles:
// Single-line comment
/*
Multi-line comment
*/
A single-line comment begins with two forward slashes: //
// This is a single-line comment
Everything after // on that line is treated as a comment.
#include <iostream>
int main() {
// Display a message
std::cout << "Hello C++";
return 0;
}
The comment explains the purpose of the output statement.
A single-line comment can also be placed after a statement.
int age = 20; // Store the age
The compiler processes the statement and ignores the comment.
int age = 20; // Student's age
double fee = 5000; // Course fee
Comments can explain what different variables represent.
A multi-line comment begins with /* and ends with */. It can span multiple lines.
/*
This is a
multi-line comment
*/
#include <iostream>
/*
This program displays
a welcome message.
*/
int main() {
std::cout << "Welcome to C++";
return 0;
}
Multi-line comments are useful when you want to describe a larger section of code.
/*
Calculate the total
and display the result.
*/
int total = price + tax;
Comments are not executed by the program.
// std::cout << "Hello";
The output statement above is commented out, so it does not execute.
Comments can be used to temporarily prevent a line of code from executing.
#include <iostream>
int main() {
std::cout << "Hello";
// std::cout << "This line is disabled";
return 0;
}
The commented statement is ignored.
int marks = 80;
// Check whether the student passed
if (marks >= 40) {
std::cout << "Pass";
}
The comment explains the purpose of the condition.
A comment can be placed before the code it describes.
// Calculate the sum
int sum = a + b;
This style can make the purpose of the following statement clear.
A short comment can be placed at the end of a statement.
int result = a + b; // Add two numbers
This is useful when the explanation is short.
Comments can explain the purpose of a function.
// Calculate the sum of two numbers
int add(int a, int b) {
return a + b;
}
for (int i = 1; i <= 5; i++) {
// Display the current number
std::cout << i << std::endl;
}
The comment describes what happens inside the loop.
if (age >= 18) {
// Person is an adult
std::cout << "Adult";
}
Comments can explain the purpose of a condition.
You can write multiple single-line comments one after another.
// Program name: Student
// Store student age
// Display student age
int age = 20;
std::cout << age;
Single-line and multi-line comments can be used in the same program.
// Program starts here
/*
Store and display
student information.
*/
int age = 20;
std::cout << age;
Comments should help readers understand the code. Good comments explain the purpose or reasoning behind code when that information is not obvious from the code itself.
// Calculate the final price after applying the discount
double finalPrice = price - discount;
Not every line of code needs a comment. A comment that simply repeats what the code already clearly says may not add useful information.
int age = 20; // Set age to 20
For simple and obvious code, meaningful variable names can often make the purpose clear.
Comments can provide useful documentation about a program, function, algorithm, or important implementation decision.
/*
Calculate the average of
three student marks.
*/
double average = (a + b + c) / 3.0;
Comment markers inside a string literal are treated as characters in the string rather than as comment syntax.
std::cout << "This is not a // comment";
Here, // is part of the displayed string.
A multi-line comment starts with /* and continues until the first matching */.
/*
This entire section
is a comment.
*/
std::cout << "Hello";
Anything between the delimiters is treated as part of the comment.
Beginners may make mistakes such as:
#include <iostream>
int main() {
int marks = 75;
// Check whether the student has passed
if (marks >= 40) {
std::cout << "Student Passed";
}
return 0;
}
The comment explains the purpose of the condition.
In larger programs, comments can help organize sections of code.
// Student Information
int age = 20;
double marks = 85.5;
// Display Information
std::cout << age;
std::cout << marks;
Useful section comments can make longer files easier to navigate.
| Type | Syntax | Use |
|---|---|---|
| Single-Line | // comment |
Short explanations or one-line comments |
| Multi-Line | /* comment */ |
Comments covering multiple lines |
#include <iostream>
/*
This program demonstrates
comments in C++.
*/
int main() {
// Store the student's marks
int marks = 80;
// Check the result
if (marks >= 40) {
std::cout << "Pass";
} else {
std::cout << "Fail";
}
return 0;
}
This example uses both single-line and multi-line comments to explain different parts of the program.
Question: Which symbol is used to start a single-line comment in C++?