C++ Comments
Comments are notes in the code. The compiler ignores them. They explain the code to humans.
Single line comment
#include <iostream>
int main() {
// This line is a comment
std::cout << "Hello\n"; // comment after code
}
Output
Hello
Multi line comment
#include <iostream>
int main() {
/* A comment on
more than one line */
std::cout << "Done\n";
}
Output
Done
Multi line comments can not be nested. A /* inside a comment does not start a new one, and the first */ ends the comment.
Disable code
#include <iostream>
int main() {
std::cout << "One\n";
// std::cout << "Two\n";
std::cout << "Three\n";
}
Output
One
Three
Good comments
- Explain why, not what. The code says what.
- Keep them short and true: update them when the code changes.
- Use clear names, so you need fewer comments.
- Many teams use documentation comments (
/** ... */with Doxygen) before functions and classes.
Note:
In C++ you can also use #if 0 ... #endif to disable a big block which already has comments inside. Next: C++ Input and Output.
Congratulations! Chapter Finished. Learn more about the similar topics:
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |