In this tutorial, you’ll learn about Comments in C++, its use cases and how to use them in your C++ program.
What are Comments?
Comments are note that is inserted directly in to the source code or C++ program. This helps the developers or anyone who is reading the source code. The comments are usually ignored by the C++ compiler.
C++ supports two types of comments
- Single line comments
- Multi line comments
Single line comments in C++
Single line comments in C++ starts with //. This tells the C++ compiler to ignore everything from the // symbol to the end of the line.
// declaring a variable int var1;
In the above program, we have used a single line comment // declaring a variable.
As a best practise, the single line comments are placed above the code as shown above. But the developers can also place the single line comments in the same line as the code on the right hand side like the one shown below.
std::cout << "Hello world!\n"; // This is a Hello World
Multi line comments in C++
In C++, multi line comments start with /* and end with */. Everything in between the symbols is ignored.
/* This is a multi-line comment. This will be ignored by the compiler */ int var;
Note: You cannot nest comments inside the multi-line comments.
For example, the below will result in an error.
/* This is a multi-line /* comment */ this will result in an error */