C++ Comments
Comment/Remark
A Comment or a Remark is text that is ignored by the compiler yet is beneficial to programmers. Code is usually annotated with comments for future reference. They are treated as white space by the compiler. In testing, you may use comments to make particular lines of code inactive; however, the #if / #endif preprocessor directives are superior for this since you can surround code with comments but not nest them. Comments are a crucial aspect of excellent documentation.
- A remark in computer programming is a programmer-readable explanation or annotation in a computer program's source code.
- Comments are statements that the compiler and interpreter do not execute.
There are two types of Comments in C++:
1. Single Line Comment
Any sequence of characters after the / (two slashes) characters. This type of comment is terminated by a new line that is not immediately beyond the backslash. As a result, it is known as "single-line comment / remark".
Example of a program containing single line comment:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a = 5; // a is a variable
cout<<”Value of a is: “<<a<<endl;
return 0;
}
OUTPUT:
Value of a is : 5
Explanation:
In the above example of a code printing value of a variable having a single line comment “// a is a variable” in the sixth line of code.
2. Multiple Line Comment
The */ characters, followed by any sequence of characters (including new lines), then the /* (slash, asterisk) characters. This syntax is identical to that of ANSI C.
Example of a program containing multiple line comment:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
/* declaring and
print the vale of the variable a. */
int a = 14;
cout<<"Value of a is: <<a<<endl;
return 0;
}
OUTPUT:
Value of a is: 14
Explanation:
In the above example of a code printing value of a variable having multiple line comment ”/* declaring and print the value of the variable a. */” in 6th and 7th line of code.
NOTE: Comments can also be used to deactivate code, making it unavailable for execution. As an example:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << "JAVATPOINT ";
// cout << "Hello world !";
cout << "for C++"<<endl;
return 0;
}
OUTPUT:
JAVATPOINT for C++
Explanation:
In the above example of code printing “JAVATPOINT for C++” in which 6th line containing single line comment which is deactivated, making it unavailable for execution.
Tip: Remember the comment shortcut; it can be really useful. It's Ctrl + / for Windows and Cmd + / for Mac in most coding editors.
What are the benefits of using comments?
- If there are no comments on the program's intricacies, a person reading a huge code will be perplexed.
- Comments are a means to provide extra description to a code to make it more understandable.
- To make code more intelligible, comments might provide a description of an algorithm.
- If code is to be reused over a lengthy period of time, comments might be useful for one-self as well.
NOTE: Comments should not be used to clarify badly written code written in English. Always write code that is well-structured and self-explanatory. Then there's the utilization of comments.