C++ Comments
A comment is a piece of text in your source code that the compiler completely ignores. Comments don’t affect how your program runs — they exist purely to help humans (including your future self) understand the code.
Single-Line Comments
A single-line comment starts with two forward slashes, //, and runs to the end of that line. Everything after // on the same line is ignored by the compiler.
// This whole line is a comment
int age = 25; // This part after the code is also a comment
Multi-Line Comments
A multi-line comment starts with /* and ends with */. Everything between those markers is ignored, even if it spans several lines.
/* This is a comment
that spans
multiple lines */
You can also write a /* */ comment on a single line, the same way you would use //.
Example
Here is a complete program that uses both comment styles:
#include <iostream>
using namespace std;
int main(void) {
// This is a single-line comment
cout << "Hello, World!" << endl; // prints a greeting
/* This is a multi-line comment.
It can span multiple lines
and is often used for longer explanations. */
cout << "Comments do not affect the output." << endl;
return 0;
}
Output:
Hello, World!
Comments do not affect the output.
Why Use Comments?
Comments are useful for:
- Explaining why a piece of code exists, not just what it does
- Leaving notes for yourself or teammates who read the code later
- Temporarily disabling a line of code while testing, without deleting it
Good comments explain reasoning that isn’t obvious from the code itself. Avoid comments that just restate the code, such as // declare an integer above int x; — they add clutter without adding information.
How It Works
Before your code is compiled, the compiler strips out every comment as if it were never there. That means comments have zero effect on your program’s size, speed, or behavior — they exist purely for readability.
Now that you know how to document your code with comments, the next lesson introduces variables, where you’ll start storing and working with data.
