Topic 5 of 83
Comments
Overview
Comments are text ignored by the compiler. They are crucial for documenting code, explaining complex logic, and temporarily disabling code during debugging.
Syntax
cpp
// This is a single-line comment
/*
This is a multi-line comment.
It can span multiple lines.
*/Common Pitfalls
- Nesting multi-line comments `/* /* */ */` is not allowed in standard C++ and will cause a compilation error.
Interview Tips
- Good code is self-documenting. Use comments to explain 'why' you did something, not 'what' the code is doing (unless the 'what' is highly complex algorithmically).
Real-World Example
Commenting out a block of code during debugging.
example
cpp
int main() {
int a = 5;
// int b = 10; // Temporarily disabled
/*
cout << "This entire block is ignored." << endl;
cout << "Useful for testing alternative solutions." << endl;
*/
return 0;
}