Comments are an essential part of a good source code. In this tutorial, we will learn about writing comments in C++ code.
What is a Comment?
Comments in programming are text notes that programmers include in their code to explain its purpose or make it easier to understand.
- They are not executed by the computer and are intended for other developers.
- Comments improve code readability and help with debugging and collaboration.
There are two types of comments in C++ programming
- Single line comment
- Multi-line comment
Let’s discuss each of these comment types with examples.
1. Single-line C++ comments
Single-line comments in C++ are denoted by double slashes (//). Any text that follows the double slashes on the same line is considered a comment and is ignored by the compiler.
#include <iostream>
using namespace std;
int main()
{
// This is a single line comment
cout<<"Hello!"; // welcome message
return 0;
}
In the above code line number 7 is a single-line comment. We can also write code and comment in a single line as shown in line number 8.
cout<<"Hello!"; // welcome message
Let’s see another example of the factorial of a number.
// Calculate the factorial of a given number using recursion
int factorial(int n) {
if (n <= 1) {
return 1; // Factorial of 0 and 1 is 1
}
else {
return n * factorial(n - 1); // Recursive calculation
}
}
Here line number 1,3 and 6 contains single line comment.
2. Multi-line C++ comments
In C++, multiline comments are used to add explanatory or descriptive text within the code. They start with /* and end with */, allowing multiple lines of comments. These comments are ignored by the compiler and are used for documentation purposes.
Here is an example of multi-line comment in C++.
#include <iostream>
using namespace std;
int main()
{
/*
This is a
multi line comment
used in C++ programminga
*/
cout<<"Hello!";
return 0;
}
Here line number 7 to 11 are multi-line comment.
Doxygen Style Comments
Doxygen is a documentation generator tool used to generate documentation for software projects. It extracts information from source code and generates various types of documentation, including HTML, PDF, and XML. It helps developers communicate and share information about their code.
Single-line comments
To add a doxygen-style single-line comment in C++, you can use the /// syntax before the comment. For example:
/// This is a doxygen-style single line comment.
Multi-line comments
Multi-line comments comment starts with “/*“, and ends with “*/“. We can write a doxygen-style multi-line comment as shown below :
/** * This is a Doxygen-style multi-line comment in C++.
* It is commonly used to provide documentation for functions, classes, and variables.
* The comment starts with "/**", ends with "*/", and can be used to generate documentation using Doxygen tool.
*/
Commenting Guidelines
It is best to consider the following points when commenting.
- Be concise: Keep comments brief and to the point.
- Explain why, not what: Explain why we selected a particular approach.
- Update comments: As code evolves, remember to update or remove outdated comments that no longer reflect the code’s functionality.
- Avoid redundant comments: Don’t comment on every line. If it is necessary then write the comment.
- Use consistent style: Follow a consistent commenting style throughout your codebase for a polished look.
Conclusion
In conclusion, we have learned to write C++ comments in code. You can read about How To Get the Type of a Variable in Cpp.