Menu

C++ Comments: Single-Line // and Multi-Line /* */

How to write comments in C++ - single-line // notes and multi-line /* */ blocks - plus how to comment out code, why block comments don't nest, and what makes a comment worth keeping.

This page includes runnable editors - edit, run, and see output instantly.

What Comments Are For

A comment is text in your source code that the C++ compiler ignores completely. It never reaches the compiled program - it exists purely for humans reading the code. You use comments to explain why something is done a certain way, to leave reminders, or to temporarily disable code without deleting it.

Building on the C++ syntax you already know, comments are one of the few things you can put almost anywhere and the compiler will simply skip them. C++ has two kinds: single-line (//) and multi-line block comments (/* */).

Single-Line Comments

Two forward slashes (//) start a comment that runs to the end of the current line. The compiler skips everything from the // to the line break.

Notice the second comment shares a line with real code. Everything before // still runs; only the part after it is ignored. This is the most common comment style for short notes.

Multi-Line Block Comments

When your note spans several lines, a block comment is cleaner than prefixing every line with //. A block comment starts with /* and ends with */. Everything between the two - however many lines - is ignored.

The aligned * characters down the leading edge are a style convention, not a rule. The only parts that actually matter are the opening /* and the closing */. A block comment can even sit in the middle of a line - int x = 5 /* width */ + 2; is valid - though that quickly gets hard to read.

Commenting Out Code

Comments are the standard way to disable code while you experiment, without deleting it. Use // for a single line, or a block comment to switch off several lines at once.

Run it and you will see only the two "runs" lines print. The commented-out cout statements are invisible to the compiler.

The No-Nesting Gotcha

A trap that bites beginners: block comments do not nest. The first */ closes the comment, no matter how many /* came before it. So you cannot wrap a /* ... */ block inside another /* ... */ block - the inner */ ends the whole thing, and everything after it becomes code again (usually a syntax error).

/*  Trying to disable a region that already has a block comment...
    int n = 10; /* the width */
    cout << n;
*/  // the first */ above already closed the comment - this line is now stray code

The outer comment ends at the */ after the width, so the rest is no longer commented out and the compiler chokes on the trailing */. When you need to disable a region that already contains block comments, use // on each line instead. Most editors toggle line comments across a whole selection with a single keyboard shortcut, which sidesteps the problem entirely.

Good Comments vs. Noise

A comment should explain something the code cannot say on its own. Comments that merely restate the code add clutter and tend to drift out of date as the code changes.

// Bad: just repeats what the code obviously does
i = i + 1; // add one to i

// Better: explains the reason, which the code can't show
retries++; // back off and retry; the API is rate-limited at 5 req/sec

Aim to make the code readable through clear names and structure, and save comments for the why - intent, trade-offs, edge cases, and links to context. If you find yourself writing a comment to explain a confusing line, that is often a hint to rename a variable or split the logic into a well-named function instead.

Next: Variables

Now that you can annotate your code, the next building block is storing data in it. The next page covers variables - how to declare them, the built-in types they hold, and the rules C++ enforces because it is statically typed.

Frequently Asked Questions

How do you write a comment in C++?

Use // for a single-line comment - everything after it on that line is ignored by the compiler. For a comment that spans multiple lines, wrap the text between /* and */. For example: // this is a note or /* this spans lines */.

What is the difference between // and /* */ in C++?

// comments out the rest of a single line, so you need one on every line. /* */ is a block comment that starts at /* and runs until the next */, even across many lines. Use // for short inline notes and /* */ to disable a chunk of text or code at once.

Can you nest comments in C++?

No. Block comments (/* */) do not nest - the first */ closes the comment no matter how many /* came before it. To disable a region that already contains block comments, put // on each line instead (most editors do this with one shortcut).

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED