Skip to content
C++ Better Explained
Go back
C++ Comments Explained: Single-Line, Multi-Line, and When to Use Them
Edit page

C++ Comments Explained

A comment is a note you leave in your code for humans to read — the compiler ignores it completely. Comments don’t change what your program does, but they make it far easier to understand later. Here’s how to write them in C++ and, just as important, when you actually should.


Single-Line Comments with //

The most common comment starts with two forward slashes, //. Everything from the slashes to the end of that line is ignored:

#include <iostream>

int main() {
    // This line greets the user
    std::cout << "Hello, world!\n";  // a comment can also sit after code
    return 0;
}

Notice you can put a // comment on its own line or at the end of a line of code. Both are fine. The comment simply stops at the line break — the next line is back to being real code.


Multi-Line Comments with /* */

When a note is longer than one line, wrap it in /* and */. Everything between them is ignored, no matter how many lines it covers:

#include <iostream>

int main() {
    /*
       This program prints a welcome message.
       I'm writing it while learning how comments work.
    */
    std::cout << "Welcome!\n";
    return 0;
}

This style is handy for a longer explanation at the top of a file, or for temporarily switching off a chunk of code while you test something.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

Commenting Out Code While You Debug

One of the most useful tricks for beginners is to comment out a line so the compiler skips it without you deleting it:

#include <iostream>

int main() {
    int price = 100;
    // int discount = 20;   // disabled while testing the full price
    std::cout << "Price: " << price << "\n";
    return 0;
}

The discount line is still there for reference, but it no longer runs. When you want it back, just remove the //.


What Comments Are Actually For

Here’s the part most tutorials skip. A good comment explains why, not what. The code already shows what it does; your comment should add the reasoning that the code can’t express on its own:

#include <iostream>

int main() {
    int celsius = 30;
    // Standard formula: multiply by 9/5, then add 32
    int fahrenheit = celsius * 9 / 5 + 32;
    std::cout << fahrenheit << "\n";
    return 0;
}

A comment like // add 1 to i next to i = i + 1 is just noise — the code already says that. A comment that explains a formula, a tricky edge case, or a business rule is genuinely valuable. When in doubt, prefer clear variable names over comments: well-named code often needs no explanation at all.


A Common Gotcha: You Can’t Nest /* */

A block comment ends at the first */ it sees. That means you cannot put one /* */ inside another — the first closing */ ends the whole thing, and the leftover text becomes code (usually causing an error). Single-line // comments, though, sit happily inside a block:

#include <iostream>

int main() {
    /* outer note — a // here is perfectly fine */
    std::cout << "Compiles cleanly\n";
    return 0;
}

If you ever need to disable a large region that already contains /* */ comments, comment out each line with // instead.


Quick Reference

GoalSyntax
Short note on one line// your note
Note after some codecode; // your note
Note spanning many lines/* your note */
Temporarily disable codeput // in front of the line
Explain reasoningcomment the why, not the what


Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 **[Get the C++ Better Explained Ebook — $19](https://start.cppbetterexplained.com/tw-sales-p

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Previous Post
How to Find the Size of an Array in C++ (Length of an Array)
Next Post
FizzBuzz in C++: The Classic Beginner Problem Explained Step by Step