comments in C++

Comments in C++: Single-Line & Multi-Line with Examples

Comments in C++ are pieces of text inside source code that the compiler completely ignores when the program is built. Their entire purpose is to communicate with human readers, not with the computer, whether that’s explaining why a piece of logic exists, warning future editors about a tricky detail, or temporarily disabling a line of code without deleting it.

Single-Line Comments

A single-line comment starts with two forward slashes, //, and continues to the end of that line. Everything after the slashes on that same line is ignored by the compiler:

// This program calculates the area of a rectangle
int length = 10;
int width = 5;
int area = length * width;  // area = length x width

Notice the second use above: a single-line comment can also appear at the end of a line of actual code, rather than only on its own line, letting a programmer annotate a specific statement directly.

Multi-Line Comments

For longer explanations spanning several lines, C++ provides a multi-line comment, opened with /* and closed with */. Everything between those two markers is ignored, no matter how many lines it spans:

/*
This program demonstrates basic input and output
in C++ using cin and cout. It reads a user's name
and prints a personalized greeting.
*/
#include <iostream>
using namespace std;

A multi-line comment can also be written on a single line, which is common for brief inline notes: /* temporary fix, revisit later */.

A Common Mistake: Nested Multi-Line Comments

C++ does not support nesting one multi-line comment inside another. The compiler closes a multi-line comment at the very first */ it encounters, so an attempt to nest one produces broken code rather than a properly nested comment:

/*
This is the outer comment.
/* This inner comment breaks things */
This line is no longer inside any comment!
*/

In the example above, the comment actually closes right after “breaks things”, leaving the remaining lines to be read as ordinary code, which will usually cause a compilation error.

What Good Comments Are Used For

Experienced programmers generally reserve comments for explaining the reasoning behind code rather than restating what the code already makes obvious. A comment like // add one to i next to i++; adds nothing a reader couldn’t already see; a comment explaining an unusual choice, such as why a loop starts at index 1 instead of 0 for this particular case, adds real value. Comments are also used to temporarily disable, or “comment out,” a line of code during testing and debugging, without deleting it outright:

int total = price * quantity;
// int discount = total * 0.1;  // discount logic disabled for now
cout << total;

Documentation Comments

Larger C++ projects often use a more structured style of comment, placed directly above a function or class, to describe what it does, what parameters it expects, and what it returns. Tools such as Doxygen can automatically read these structured comments and generate readable documentation pages from them, which is why many teams follow a consistent comment format above every function rather than writing free-form notes.

Best Practices for Writing Comments

Good comments stay accurate as code changes; a comment left behind after the logic it describes has been rewritten is worse than no comment at all, since it actively misleads whoever reads it next. Comments are best kept concise, focused on the “why” rather than the “what,” and updated in the same commit or edit that changes the code they describe, so that code and comment never drift apart.

How Comments in C++ Affect Compilation

It’s worth understanding exactly when comments in C++ disappear from a program. The compiler does not skip over comments at the same stage it checks your logic – they are stripped out earlier, during preprocessing, before the actual compilation of your code even begins. This means a comment has zero effect on the size or speed of the compiled program; a heavily commented file and its stripped-down equivalent produce identical machine code. It also means a comment can never accidentally change how a program behaves, which is precisely why comments are considered safe to add liberally without any risk to a program’s correctness, unlike almost every other line in a C++ file.

For the next step in this series, see our guide on keywords, identifiers and Variables in C++. For the full, authoritative language reference, cppreference.com documents this in complete technical detail.

No Comments

Leave a Reply

Recent Comments

No comments to show.