In this tutorial, we will learn about C++ comments, why we use them, and how to use them with the help of examples.
C++ comments are hints that a programmer can add to make their code easier to read and understand. They are completely ignored by C++ compilers. They are added with the purpose of making the source code easier to read for the people who are from non-coding background.
There are two ways to add comments to code:
// - Single Line Comments /* */ - Multi-line Comments
In C++, any line that starts with // is a comment. For example,
// declaring a variable int x; // initializing the variable 'x' with the value 5 x = 5;
Here, we have used two single-line comments:
// declaring a variable
// initializing the variable 'x' with the value 5
We can also use single line comment like this:
int x; // declaring a variable
In C++, any line between /* and */ is also a comment. For example,
/* declaring a variable to store salary to employees */ int sal = 5000;
This syntax can be used to write both single-line and multi-line comments.
Comments can also be used to disable code to prevent it from being executed. For example,
#include <iostream>
using namespace std;
int main() {
cout
If we get an error while running the program, instead of removing the errorprone
code, we can use comments to disable it from being executed; this
can be a valuable debugging tool and save time to delete the wrong code
and to rewrite it.
#include <iostream>
using namespace std;
int main() {
cout
Pro tip: Remember the shortcut for using comments; it can be really
helpful. For most code editors, it's Ctrl + / for Windows and Cmd + / for
Mac.
Why use Comments?
If we write comments on our code, it will be easier for us to understand the
code in the future. Also, it will be easier for your fellow developers to
understand the code.
Note: Comments shouldn't be the substitute for a way to explain poorly
written code in English. We should always write well-structured and selfexplanatory
code. And, then use comments.
As a general rule of thumb, use comments to explain Why you did
something rather than How you did something, and you are good.