Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Foundation  ·  Lesson 06

Comments in C++

In this lesson: Write single-line (//) and multi-line (/* */) comments in C++ with examples and best practices. In Hindi & English.

What is a comment?

A comment is text in your source code that the compiler ignores. It is written for humans — your teacher, your teammates, or you in six months — to explain what the code is for and why it is written that way. Before compilation, the preprocessor strips every comment out, so comments never affect how the program runs.

Comments explain intent. The code already says *what* happens; a good comment says *why*.

Single-line comments: //

Everything from // to the end of that line is a comment. It can sit on its own line or after a statement.

C++
#include <iostream>

int main() {
    // Print a welcome message for the class
    std::cout << "Welcome to C++!\n";

    int passMarks = 33;   // CBSE minimum pass percentage
    std::cout << "Pass marks: " << passMarks << "\n";
    return 0;
}
Welcome to C++! Pass marks: 33

Single-line comments are the most common kind in modern C++ code.

Multi-line (block) comments: /* ... */

Text between /* and */ is a comment, even across many lines. It is handy for a longer explanation at the top of a file or function.

C++
/*
   Program : Student result calculator
   Author  : Aman, Class XII-A
   Purpose : Reads marks of 5 subjects and prints
             total, percentage and grade.
*/
#include <iostream>
int main() {
    int total = 0; /* running total of marks */
    return 0;
}

A block comment can even sit in the middle of a line, e.g. int x = /* default */ 10;, although that style is rarely a good idea.

Block comments do not nest

The first */ always closes the comment. This is a classic mistake:

C++
/*  Old code, disabled for now
    int a = 5;  /* temporary value */
    int b = 7;
*/

The comment ends after temporary value */, so int b = 7; becomes live code and the final */ is a syntax error. To switch off a big block that already contains comments, use the preprocessor:

C++
#if 0
    int a = 5;  /* temporary value */
    int b = 7;
#endif

Documentation comments

Tools such as Doxygen can turn specially formatted comments into HTML documentation. They are ordinary comments that start with /** or /// and use tags like @param and @return.

C++
/**
 * Calculates the percentage of obtained marks.
 * @param obtained marks scored by the student
 * @param maximum  total marks of the exam (must be > 0)
 * @return percentage as an integer from 0 to 100
 */
int percentage(int obtained, int maximum) {
    return obtained * 100 / maximum;
}

Even if you never run Doxygen, this layout makes it obvious how a function should be used.

What makes a good comment?

CommentVerdictWhy
i++; // increase i by 1AvoidRepeats the code.
// skip the header row of the CSVGoodExplains intent.
// max 100 students — the school ERP limitGoodRecords an assumption.
// TODO: handle negative marksGoodMarks unfinished work clearly.
// fixed bug (with no detail)AvoidTells the reader nothing.

Other tips:

  • Prefer clear names first; add a comment only when the name cannot say it all.
  • Keep comments up to date — an outdated comment is worse than none.
  • In exams and practical files, a short header comment (program name, purpose) and one comment per logical step is ideal.

Using comments while debugging

"Commenting out" a line is a quick way to test whether it causes a problem:

C++
// std::cout << "debug: total = " << total << "\n";

Remove such lines before you submit or share the code — leftover commented-out code confuses readers.

Common mistakes

  • Nesting /* */ comments, as shown above.
  • Forgetting to close a block comment — the rest of the file silently becomes a comment and you get confusing "expected ... at end of input" errors.
  • Writing / instead of //, which the compiler reads as division.
  • Commenting every obvious line, which hides the few comments that matter.
  • Leaving old code commented out instead of deleting it.
🏋️ Practice

Take a 20-line program you wrote (for example, a grade calculator). Add a header block comment, one // comment per logical step explaining why, and a Doxygen-style comment for one function. Then delete any comment that only repeats the code.

Summary

  • // comments run to the end of the line; /* */ comments can span lines.
  • Comments are removed before compilation and do not affect speed.
  • Block comments do not nest — use #if 0 ... #endif to disable large blocks.
  • /** */ and /// doc comments can be turned into documentation by Doxygen.
  • Comment the *why*, keep comments current, and delete dead code.

Related: structure of a C++ program, preprocessor directives, C++ best practices.

Frequently Asked Questions

How many types of comments are there in C++?
C++ has two comment syntaxes: single-line comments that start with // and run to the end of the line, and multi-line (block) comments written between /* and */. Documentation comments such as /** ... */ or /// are just these same comments written in a format that tools like Doxygen can read.
Can multi-line comments be nested in C++?
No. A block comment ends at the first */ the compiler sees, so /* outer /* inner */ still outer */ ends early and the remaining text becomes code, causing an error. To disable a large block that already contains comments, use #if 0 ... #endif instead.
Do comments slow down a C++ program?
No. The preprocessor removes comments before compilation, so they never reach the compiled program. They have zero effect on speed or file size of the executable.
What should I write in a comment?
Explain why the code does something, any assumption it relies on, and anything surprising — not what each line obviously does. A comment like // skip the header row of the CSV is useful; i++; // increase i is not.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicVariables and Data Types in C++ Your progress: 6 / 81 (7%) · Lesson 7 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free