Programming is often highly collaborative. In addition, our own code can quickly become difficult to understand when we return to it — sometimes only a few hours later! For these reasons, it’s often useful to leave notes in our code for ourselves or other developers.
As we write a C++ program, we can write comments in the code that the compiler will ignore as our program runs. These comments exist just for human readers.
Comments can explain what the code is doing, leave instructions for developers using the code, or add any other useful annotations.
There are two types of code comments in C++:
A single line comment will comment out a single line and is denoted with two forward slashes
// preceding it:
// Prints "hi!" to the terminal std::cout << "hi!";
You can also use a single line comment after a line of code:
std::cout << "hi!"; // Prints "hi!"
A multi-line comment will comment out multiple lines and is denoted with
/* to begin the comment, and
*/ to end the comment:
/* This is all commented. std::cout << "hi!"; None of this is going to run! */
Let’s practice adding a comment.
Add a new line above
Write a single line comment that says
Compile and execute spell.cpp using the terminal.
This checkpoint will pass after you compile and execute.