Multiline Comment in C++
Many developers worry that their code should be easy to read and understandable by the targeted people or when going for a review. However, many people need help to maintain it because whenever we are mastering any language or skill, we often become so focused on the huge aspects and need to recognize that our minor details and basic knowledge need more emphasis. In a programming language, comments can make your code more readable or enhance its beauty. It can also help us understand the code better and increase reusability.
What are the comments?
Comments are statements we write to explain something in our code and improve its readability and maintainability. They are not considered part of the code simply because they are not executed. They are just used to explain why we have used an element in the code. They used to document or provide annotations for the code.
What are the types of comments?
There are primarily two types of comments in the programming world. One is a single-line comment and the other one is a multi-line comment. Let’s look at them in detail: -
Single line comment
The single-line comment usually begins with two forward slashes, such as //. Anything written after these two forward slashes is not considered part of the code and is not executed. They can be used when we write short notes in the code.
Examples:
Example 1)
#include <iostream>
// This is a C++ program that calculates the factorial of a number.
// The user is prompted to enter a positive integer and the program
// calculates the factorial of that number.
// Function to calculate the factorial of a number
unsigned long long calculate factorial(int n) {
// Initialize the result to 1
unsigned long long result = 1;
// Loop from 1 to n and multiply each number by the result
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
int num;
// Prompt the user to enter a positive integer
std::cout << "Enter a positive integer: ";
std::cin >> num;
if (num < 0) {
// If the user enters a negative number, display an error message
std::cout << "Error: Please enter a positive integer." << std::endl;
} else {
// Calculate and display the factorial
unsigned long long factorial = calculate factorial(num);
std::cout << "Factorial of " << num << " is " << factorial << std::endl;
}
return 0;
}
Output:
![Multiline Comment in C++](https://static.tutorialandexample.com/cpp/multiline-comment-in-cpp1.png)
Example 2)
#include <iostream>
// Function to add two numbers
double add(double a, double b) {
return a + b;
}
// Function to subtract two numbers
double subtract(double a, double b) {
return a - b;
}
// Function to multiply two numbers
double multiply(double a, double b) {
return a * b;
}
// Function to divide two numbers
double divide(double a, double b) {
if (b != 0) {
return a / b;
} else {
std::cerr << "Error: Division by zero is not allowed." << std::endl;
return 0.0;
}
}
int main() {
char operation;
double num1, num2;
// Prompt the user to enter an operation (+, -, *, /)
std::cout << "Enter an operation (+, -, *, /): ";
std::cin >> operation;
// Prompt the user to enter two numbers
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
double result;
// Perform the selected operation
switch (operation) {
case '+':
result = add(num1, num2);
break;
Case '-':
result = subtract(num1, num2);
break;
case '*':
result = multiply(num1, num2);
break;
Case '/':
result = divide(num1, num2);
break;
Default:
std::cerr << "Error: Invalid operation." << std::endl;
return 1; // Exit with an error code
}
// Display the result
std::cout << "Result: " << num1 << " " << operation << " " << num2 << " = " << result << std::endl;
return 0;
}
Output:
![Multiline Comment in C++](https://static.tutorialandexample.com/cpp/multiline-comment-in-cpp2.png)
Example 3)
#include <iostream>
int main() {
//printing my name in the program
std:: cout <<"Hello, I am Tanya";
return 0;
}
Output:
![Multiline Comment in C++](https://static.tutorialandexample.com/cpp/multiline-comment-in-cpp3.png)
Multi-line comment
As the name suggests, multiline comments usually last for multiple lines and do not conclude within a single line. They are usually used where we have to add a detailed explanation of the code or its annotation and explain the workings of the program we are writing. They usually begin with a /* and end with a */. Anything after the “/*” element will make absolutely no sense to the compiler and will be for the understanding of the developer and better user experience. They can further contribute to some of the best coding practices.
Example
#include <iostream>
int main() {
/* printing my name in the program, and this is the best person in the world. I am going to use this information and also perform some operations such as:
A+B = C */
std:: cout <<"Hello, I am Tanya";
return 0;
}
Output:
![Multiline Comment in C++](https://static.tutorialandexample.com/cpp/multiline-comment-in-cpp4.png)
Why do we use multiple comments?
Multiline comments are a very powerful tool and can become any developers best tool when it comes to programming. Web developers write many codes daily, and the need to document them keeps increasing. We want our code to be like when anyone looks at it, they can understand it. Some of the best practices of multiline comments are stated below: -
1.Accuracy
Multiline comments should be used precisely and should not be overused. We have to be specific about why we choose to explain or document a certain part of the document.
2.Understanding
When we write comments in our code, it gives us insights into our analytical thinking. It facilitates how a given program works in this code and how the developer implements the logic. This whole procedure helps the reader also to increase their logic building.
3.Maintainability
Multiline comments help us maintain the code as they help us document the necessary parts of the program that may require some explanation for better understanding. They increase the readability of the code which is directly proportional to the maintenance of the code.
4.Use it sparingly.
We have to be very certain with the usage of multiline comments and make sure that its implementation is worthwhile. It’s important that we only write comments where it is required.
Key takeaways
This tutorial shows the basic concepts of multiline comments and how it differs from a single comment. We have delved into the fundamental concepts of the multiline comments and their usage in this article. It’s a great skill to be acquired by any developer in their daily practices. Comments can be very beneficial for any developer and can pave several ways for best coding practices. This can harness results, leading to more advanced and clean coding practices.