Comments

When writing code, it’s often useful to add “comments” that describe how some code functions. That way, the code can be easier to understand for someone else reading it or if you look at it far in the future.

In C++, there are two types of comments: multi-line and single line.

/*
This is a multi-line comment in C++.
It can span multiple lines.
*/
//This is a single line comment in C++.
//Each line must begin with two forward slashes.

As you can see, multi-line comments must be surrounded by “/*” and “*/“, while each line of a single line comment must begin with “//“.

Here’s what comments would look like in a C++ program:

/*
This program prints "Hello world!".
The text is followed by a new line.
*/

//Include iostream for std::cout
#include <iostream>

int main() {
    //Print "Hello world!" followed by a new line
    std::cout << "Hello world!\n";

    return 0;
}

Note that comments in a program have no effect on how the program is run since they’re ignored by the compiler.

Also, it’s always best to avoid writing too few or too many comments in code. With too few, code can be harder to read, and with too many (as in the program above), the comments can just be unnecessary.