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.
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
| Goal | Syntax |
|---|---|
| Short note on one line | // your note |
| Note after some code | code; // your note |
| Note spanning many lines | /* your note */ |
| Temporarily disable code | put // in front of the line |
| Explain reasoning | comment the why, not the what |
Related Articles
- C++ Hello World Explained — your first program, line by line
- Breakdown of a Simple C++ Program — what each part does
- C++ Variables and Data Types — name things clearly so you need fewer comments
- C++ Header Files — where bigger comment blocks often live
- How to Start Learning C++ — set up your environment first
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