Difficulty: Beginner
Programming Language: C++

What is a comment?

Comments provide a way for a programmer to annotate their source code to provide useful information. When the compiler parses your applications source code it will completely ignore any comments and continue parsing the rest of the current document.

Although not essential it is considered good practice to comment your code appropriately, as well commented code can be read and understood much more easily and quickly than attempting to read the source code itself.

How to create a comment in code.

C++ provides two different kinds of comment:

  • Single line comment
  • Multi line comment (or block comment)

A single line comment is started by using a sequence of two forward slashes // and this will cause the compiler to ignore everything from here until the end of the current line.

A multi line comment is started by using a sequence of /* and continues until the matching end sequence of */ is found. Because the comment continues until the closing sequence is found it is able to span across multiple lines within a source file, allowing us to give more detailed descriptions of the code.
Unlike a single line comment, a multi line comment can be terminated within the same line by using the closing sequence.

An example of both types of comment is shown below using our earlier Hello World eample.