C++ Multi-Line Comments
Multi-line comments in C++ are used to write comments that span across multiple lines. They are useful for detailed explanations, documentation blocks, or temporarily disabling large sections of code.
1. Syntax of Multi-Line Comments
In C++, multi-line comments start with /* and end with */. Everything written between these symbols is ignored by the compiler.
C++
Basic multi-line comment syntax
/*
This is a multi-line comment.
It can span multiple lines.
The compiler ignores everything inside.
*/
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
2. When to Use Multi-Line Comments
- To write detailed explanations about program logic.
- To describe functions or modules.
- To temporarily disable multiple lines of code.
- To add documentation at the top of a file.
3. Commenting Out a Block of Code
You can disable multiple lines of code using multi-line comments during debugging.
C++
Using multi-line comment to disable code
#include <iostream>
using namespace std;
int main() {
int x = 10;
/*
int y = 20;
cout << y;
*/
cout << x;
return 0;
}
4. File Header Documentation Example
C++
Multi-line comment as file documentation
/*
Program Name: Simple Calculator
Author: CodeCrown
Description: This program performs basic arithmetic operations.
Date: 18-02-2026
*/
#include <iostream>
using namespace std;
int main() {
cout << "Calculator Program";
return 0;
}
5. Important Notes
- Multi-line comments cannot be nested in C++.
- Make sure every /* has a matching */.
- Avoid using block comments to disable code that already contains block comments.
Conclusion
Multi-line comments in C++ are powerful tools for documentation and debugging. By using the /* */ syntax properly, developers can write cleaner, more maintainable, and well-documented programs.
Codecrown