100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Comments in C++

Learn what comments are in C++, the difference between single-line and multi-line comments, and best practices for using them.

BasicsBeginner5 min readJul 7, 2026
Analogies

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

cpp
// 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

cpp
#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

text
Area: 50

6. 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

Was this page helpful?

Topics covered

#CStudyNotes#Programming#CommentsInC#Comments#Syntax#Explanation#Example#StudyNotes#SkillVeris#ExamPrep