delimited comments

suggest change

A 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 between those markers is a comment. In translation phase 3, each comment is replaced by one space.

/* 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 disable a block of code that already contains /* ... */ comments, use #if 0, as described in Commenting using the preprocessor.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents