Single-line comments start with two forward slashes //. Everything after // on that line is ignored by the compiler.
// This is a single-line comment
cout << "Hello World!"; // This comment is at end of a statement
Multi-Line Comments
Multi-line comments start with /* and end with */. They can span multiple lines.
/* This is a multi-line comment.
It can span many lines.
The compiler ignores everything between the delimiters. */
cout << "Comments don't affect output!";
Full Example
#include <iostream>
using namespace std;
int main() {
// Print a greeting message
cout << "Hello!" << endl;
/* This block demonstrates
multi-line comments */
cout << "Comments are for humans, not compilers." << endl;
return 0; // End of program
}
Output:
Hello!
Comments are for humans, not compilers.
When to Use Comments
Explain complex logic that is not obvious from the code.
Temporarily disable code during debugging.
Document functions — what they do, parameters, return values.
Leave TODOs for future improvements.
Best Practice: Write comments that explain why something is done, not what is being done. Good code is self-explanatory; comments fill in the reasoning.
Exercise
Add a single-line comment above each cout line to explain what it prints.