M
0
Types of comments
A comment 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:
1
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.
1
2
3
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.
1
2
3
4
5
6
7
8
// 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.
1
2
3
/* This is a multi-line comment.
This line will be ignored.
So will this one. */
Since everything between the symbols is ignored, you will sometimes see programmers
A comment 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:
1
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.
1
2
3
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.
1
2
3
4
5
6
7
8
// 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.
1
2
3
/* This is a multi-line comment.
This line will be ignored.
So will this one. */
Since everything between the symbols is ignored, you will sometimes see programmers