1. Intro
A comment is text in your source code that the compiler completely ignores. Comments exist purely for humans — to explain what a piece of code does, leave notes for other developers, or temporarily disable a line of code without deleting it.
Cricket analogy: A comment is like a captain's handwritten note in the team's tactics folder, e.g. Rohit Sharma's plan to attack spin in the powerplay — the umpire never reads it, but teammates rely on it to understand the plan.
2. Syntax
// single-line comment
/* multi-line
comment */3. Explanation
C++ supports two comment styles. A single-line comment starts with // and continues until the end of that line — everything after // on that line is ignored by the compiler. A multi-line comment starts with /* and ends with the matching */, and can span as many lines as needed in between.
Cricket analogy: A single-line // comment is like a quick mid-over shout from the dugout, gone the moment that ball is bowled, while a /* */ block comment is like a full tea-break tactical briefing covering several points at once.
Comments are removed during the earliest stage of compilation, alongside whitespace, and never affect the compiled program's behavior or performance. Because of this, they are extremely useful for explaining *why* code does something (the reasoning), rather than restating *what* the code obviously already shows.
Cricket analogy: Comments vanish before the match starts just like pre-match warmup drills aren't part of the scorecard, and good ones explain WHY Virat Kohli chose to bat first (pitch reading) rather than WHAT he did (won the toss, batted).
Multi-line comments cannot be nested — writing /* outer /* inner */ still-outside */ will close at the first */ it finds, leaving still-outside */ as invalid code.
4. Example
#include <iostream>
using namespace std;
int main() {
// This calculates the area of a rectangle
int length = 10;
int width = 5;
/* The formula for area is:
area = length * width */
int area = length * width;
cout << "Area: " << area << endl; // print the result
return 0;
}5. Output
Area: 506. Key Takeaways
- A comment is ignored by the compiler and exists purely to help human readers.
//starts a single-line comment that ends at the end of that line./* ... */starts a multi-line comment that ends at the matching*/.- Multi-line comments cannot be nested inside one another.
Practice what you learned
1. What happens to comments when a C++ program is compiled?
2. Which symbol starts a single-line comment in C++?
3. How do you write a multi-line comment in C++?
4. Can multi-line comments be nested in C++?
5. What is the best practice for what a comment should explain?
Was this page helpful?
You May Also Like
Tokens in C++
Learn what tokens are in C++, the six kinds of tokens, and how the compiler breaks source code into them.
Escape Sequences in C++
Learn what escape sequences are in C++, the full list of common ones, and how to use them inside strings and characters.
Literals in C++
Learn what literals are in C++ — integer, floating-point, character, string, and boolean literals — with syntax and examples.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics