☜
☞
Multi-line
Multi-line comments start with /* and end with */. Any text between these delimiters is treated as a comment, and it can span multiple lines.
#include <iostream>
int main() {
/* This is a multi-line comment.
It spans several lines.
The compiler will ignore it. */
std::cout << "Hello, World!" << std::endl;
return 0;
}
In this example, the text between /* and */ is a multi-line comment.
Notes:
Comments should not be nested. While some compilers may allow it, it's not a good practice and is not universally supported.
/* This is a multi-line comment
/* This nested comment could cause problems */
*/
Be careful with commenting out code by using multi-line comments, as a */ inside a string literal may end the comment prematurely.
/*
std::cout << "This will cause a problem */";
*/
It is a good practice to add comments that explain the "why" rather than the "what" unless the code is highly unconventional or uses tricky hacks that are not immediately obvious.
Remember, good comments make it easier for others (and yourself, at a later date) to understand your code.
☜
☞