Comments in C++ are used to explain and annotate code, making it easier to understand and maintain. They are ignored by the compiler and have no effect on the program’s execution. C++ supports two types of comments: single-line comments and multi-line comments.
Single-line comments start with two forward slashes (//
). Everything following //
on that line is treated as a comment and ignored by the compiler.
#include <iostream>
int main() {
// This is a single-line comment
std::cout << "Hello, world!" << std::endl; // This comment is ignored
return 0;
}
Multi-line comments start with /*
and end with */
. They can span multiple lines.
#include <iostream>
int main() {
/*
* This is a multi-line comment
* that spans several lines.
*/
std::cout << "Hello, world!" << std::endl;
return 0;
}
Use comments to explain the purpose of the code and the logic behind complex algorithms or operations. Avoid stating the obvious, as comments should add value to the understanding of the code.
#include <iostream>
// Function to calculate the factorial of a number
int factorial(int n) {
// Base case: factorial of 0 or 1 is 1
if (n == 0 || n == 1) {
return 1;
}
// Recursive case: n * factorial of (n-1)
return n * factorial(n - 1);
}
int main() {
int number = 5;
// Calculate and print the factorial of the number
std::cout << "Factorial of " << number << " is " << factorial(number) << std::endl;
return 0;
}
Comments can be useful for code maintenance, such as marking sections that need to be reviewed or modified in the future.
#include <iostream>
int main() {
int a = 10;
int b = 20;
// TODO: Optimize the following calculation
int sum = a + b;
std::cout << "Sum: " << sum << std::endl;
return 0;
}
Too many comments can clutter the code and make it hard to read. Focus on commenting the “why” and “what” rather than the “how.”
#include <iostream>
// Avoid unnecessary comments
int main() {
int x = 10; // Declare an integer variable x and initialize it to 10
x += 5; // Add 5 to x
std::cout << x << std::endl; // Print the value of x
return 0;
}
Strive to write self-explanatory code where possible. Good naming conventions for variables and functions can reduce the need for comments.
#include <iostream>
// Function to calculate the sum of two integers
int add(int a, int b) {
return a + b;
}
int main() {
int number1 = 10;
int number2 = 20;
int result = add(number1, number2);
std::cout << "Sum: " << result << std::endl;
return 0;
}
Doxygen is a tool for generating documentation from annotated C++ source code. Special comment formats are used to create structured documentation.
#include <iostream>
/**
* @brief Function to calculate the factorial of a number.
*
* This function uses a recursive algorithm to calculate
* the factorial of a given non-negative integer.
*
* @param n The number to calculate the factorial for.
* @return The factorial of the number.
*/
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int number = 5;
std::cout << "Factorial of " << number << " is " << factorial(number) << std::endl;
return 0;
}
//
and extend to the end of the line./*
and end with */
, can span multiple lines.