Comments in C

(Last Mod: 27 November 2010 21:38:38 )

ECE-1021 Home


Block Comments

Block comments were originally the only comments recognized by C and, while most compilers quickly included extensions to the language that recognized end-of-line comments, these were not part of the ANSI C Standard until C99 was adopted.

Block comments begin with a forward slash, asterisk character combination, "/*", and end with the next occurring asterisk, forward slash combination, "*/". Everything in between the "comment start mark" and the "comment stop mark" is ignored by the compiler. It's important to note that these comment blocks cannot be "nested" - meaning to place one inside another. The compiler is quite literal-minded and it means precisely what is said above - that once it sees the comment-start mark it will ignore everything, including subsequent comment start marks, until the first time it encounters a comment stop mark. If it then encounters another comment stop mark, it will throw an error.

Many programmer's use the slang "slash-splat" for the first pair and "splat-slash" for the second - usually in conjunction with an appropriate hand gesture.

End-of-Line Comments

Frequently it is desired to place a short comment after a line of code or to only have a comment that is one line in length. The need to provide both start and stop marks for these kinds of comments can get quite cumbersome. Hence most compilers began recognizing a different kind of comment start mark - the double forward slash "//". For this type of comment, the corresponding comment stop mark is the new line character that is always located at the end of each line in an ASCII text file (assuming the editor doesn't automatically wrap lines if they are too long).

Since this type of comment was not included as part of ANSI C until C99, any compiler that is pre-C99 and that is configured to enforce ANSI compliance will treat these comments as errors since other pre-C99 ANSI-compliant compilers may or may not recognize them.

As with block comments, the compiler is very literal minded with these comments as well. Hence you can "comment out" a block comment start mark if it appears on the same line, but after, an end-of-line comment mark.

Which Comment Style to Use

While the End-of-line comments are quite a bit more convenient in many situations, they are not as portable. The reason is that relatively few C99 compliant compilers are in widespread use and many programmers set their compiler options to be "strictly conforming" to the ANSI standard which, in most cases, is the older C89 standard. This provides the greatest possibility to make their code portable to other compilers and platforms and the only sacrifice ANSI conformance for good reason - while good reasons do exist and are actually quite common, the convenience of being able to use end-of-line comments is not considered one of them.

Your code is expected to be ANSI C89-compliant unless the problem statement says or implies otherwise. This means you must limit yourself to block comments.

If you are expected/required to use functions from the <conio.h> library, then your code must use the Borland Extensions and will not be compliant.