delimited comments
suggest changeA comment starts with a forward slash followed immediately by an asterisk (/*
), and ends as soon as an asterisk immediately followed by a forward slash (*/
) is encountered. Everything in between these character combinations is a comment and is treated as a blank (basically ignored) by the compiler.
/* this is a comment */
The comment above is a single line comment. Comments of this /*
type can span multiple lines, like so:
/* this is a
multi-line
comment */
Though it is not strictly necessary, a common style convention with multi-line comments is to put leading spaces and asterisks on the lines subsequent to the first, and the /*
and */
on new lines, such that they all line up:
/*
* this is a
* multi-line
* comment
*/
The extra asterisks do not have any functional effect on the comment as none of them have a related forward slash.
These /*
type of comments can be used on their own line, at the end of a code line, or even within lines of code:
/* this comment is on its own line */
if (x && y) { /*this comment is at the end of a line */
if ((complexCondition1) /* this comment is within a line of code */
&& (complexCondition2)) {
/* this comment is within an if, on its own line */
}
}
Comments cannot be nested. This is because any subsequent /*
will be ignored (as part of the comment) and the first */
reached will be treated as ending the comment. The comment in the following example will not work:
/* outer comment, means this is ignored => /* attempted inner comment */ <= ends the comment, not this one => */
To comment blocks of code that contain comments of this type, that would otherwise be nested, see the https://stackoverflow.com/documentation/c/10670/comments/32034/commenting-using-the-preprocessor example below