Types of comments
is a line (or multiple lines) of text that are inserted into the source code to explain what the code is doing. In C++ there are two kinds of comments.
The // symbol begins a C++ single-line comment, which tells the compiler to ignore everything to the end of the line. For example:
cout << "Hello world!" << endl; // Everything from here to the right is ignored.
Typically, the single line comment is used to make a quick comment about a single line of code.
cout << "Hello world!" << endl; // cout and endl live in the iostream library
cout << "It is very nice to meet you!" << endl; // these comments make the code hard to read
cout << "Yeah!" << endl; // especially when lines are different lengths
Having comments to the right of a line can make the both the code and the comment hard to read, particularly if the line is long. Consequently, the // comment is often placed above the line it is commenting.
// cout and endl live in the iostream library
cout << "Hello world!" << endl;
// this is much easier to read
cout << "It is very nice to meet you!" << endl;
// don't you think so?
cout << "Yeah!" << endl;
The /* and */ pair of symbols denotes a C-style multi-line comment. Everything in between the symbols is ignored.
/* This is a multi-line comment.
This line will be ignored.
So will this one. */
Multi-line style comments do not nest. Consequently, the following will have unexpected results:
/* This is a multiline /* comment */ this is not inside the comment */
// ^ comment ends here
Rule: Never nest comments inside of other comments.