In this tutorial let us learn about C++ comments, why we use them, and how to use them with examples.
C++ comments are suggestions that a programmer can add to their code to make it easier to read and understand. C++ compilers will completely ignore them. Comments can be used to explain and improve the readability of C++ code. It can also be used to prevent execution when experimenting with different codes.
Comments shouldn't be used as a stand-in for a way to explain English-language code that isn't written well. We should always write code that is well-structured and self-explanatory. Then, use comments.
Comments are text notes that are inserted into the program to explain the source code. They are used in a programming language to document the program and remind programmers of the tricky things they just did with the code. They also aid in the understanding and maintenance of code for future generations.
There are mainly two methods for adding comments to code:
The //(two slashes) characters, followed by any character sequence. This type of comment is terminated by a new line that is not immediately followed by a backslash. As a result, it is mainly referred to as a "single-line comment."
// comment
Any line in C++ that begins with // is a comment.
For example,
// declaring a variable
int a;
// here we are initializing the variable 'a' with the value 5
a = 5;
We've used two single-line comments here:
You can also include a comment at the end of a line of code. However, it is generally preferable to place the comment before the line of code. This example given below applies to both C and C++ because the commenting style is the same in both languages.
We can also use a single-line comment, such as :
int a; // declaring a variable
Any text can be represented as /* any text */. Begin with a forward slash and an asterisk (/*) and end with a forward slash and an asterisk (*/).
It's used to indicate a multi-line comment. It is possible to apply the comment to more than one line. It is known as a C-Style comment because it was first used in C programming.
/*Comment starts
continues
continues
.
.
.
Comment ends*/
Any line between /* and */ in C++ is also a comment.
For an example
/* C++ program in order to illustrate
use of
multi-line comment */
#include <iostream>
int main()
{
/* Multi-line will Welcome user comment
written to demonstrate comments
in C++ */
std::cout << "Welcome to learn eTutorials";
return 0;
}
// This code is contributed by lasepra123
Output:
Welcome to learn eTutorials
Comments can also be used to disable code so that it is not executed.
For example,
#include <iostream>
using namespace std;
int main() {
cout << "some code";
cout << ''error code;
cout << "some other code";
return 0;
}
If we get an error while running the program, rather than removing the error-prone code, we can use comments to prevent it from being executed; this can be a useful debugging tool.
#include <iostream>
using namespace std;
int main() {
cout << "some code";
// cout << ''error code;
cout << "some other code";
return 0;
}
}