Comments
Comments
Comments are lines or blocks of text used to document a program's functionality and explain how a program works for the benefit of a programmer. Comments are ignored by the compiler, so you can type anything you want into them.

C supports two different styles of comments: Block Comments and Single Line Comments.

Block Comments

/* This is a comment */

Block Comments begin with a /* and end with a */ and may span multiple lines. For example:

Lines 1-4 comprise one long comment that spans four lines. It begins with the /* at the beginning of line 1 and ends with the */ at the end of line 4. Every other character between those two delimiters is ignored by the compiler.

Line 7 shows another example where a block comment is used for just a single line. While line 10 shows a similar construct to add a comment at the end of a line of code.

Single Line Comments

// This is also a comment

Single line comments begin with a // and run to the end of the line. These are sometimes called "C++ style comments" because they were developed for C++ and were not originally part of the ANSI C standard. They may not span multiple lines. For example:

Here we have replaced each block comment from the first example with an equivalent form using a line comment.

Lines 1-4 are now four separate comments with each line starting with //.

Lines 7 and 10 were also converted into single line comments, which are more appropriate for their situations.

Nesting Comments

Block comments may not be nested within other block comments. However, single line comments may be nested within a block comment.

Lines 1-3 show that it is perfectly valid to nest a single line comment within a block comment. In fact, this is a very common practice when testing programs. From time to time you may want to temporarily remove a block of code from your program. Using block comment delimiters as shown here will have that effect as long as the only other comments between the delimiters are single line comments.

Lines 5-8 show what can go wrong when block comments are nested within one another. A comment begins with the /* at the beginning of line 5. Because of line 5, the opening delimiter /* on line 6 is completely ignored, and the closing delimiter */ at the end of line 6 is matched with the opening delimiter on line 5. This leaves the code on line 7 uncommented, followed by a complete comment at the end of line 7.

Finally at the beginning of line 8 we have a closing delimiter */ that is unmatched with an opening delimiter, which will cause a syntax error.

Best Practices

Ideally you should use single line comments throughout most of your code, reserving block comments for times when you want to temporarily remove a block of code or for when you really need multi-line comments. Below is an example of how many programmers use the two comment styles.

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.