capypad
0 day streak
cpp / beginner
Snippet

Comments: Documenting Your Code

Comments are ignored by the compiler and serve as documentation. Single-line comments start with //. Multi-line comments are wrapped in /* */. Good comments explain why, not just what - they help others (and your future self) understand your code.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
int main() {
int base = 5;
int height = 3;
// Calculate area of triangle
double area = 0.5 * base * height;
/* Multi-line comment:
This formula calculates the area
using the standard geometric formula */
std::cout << "Area: " << area << std::endl;
return 0;
}
Breakdown
1
// Calculate area of triangle
Single-line comment describing the purpose of the next line
2
/* Multi-line comment: ... */
Multi-line comment spanning several lines for detailed explanations