C++ single line comments
Created By: chatGPT
In C++, single line comments start with two forward slashes
//
. Anything following the //
on that line will be considered a comment and will not be executed as code.// This is a single line comment in C++
int main() {
// Initialize variable
int x = 5; // Assign value
return 0; // End of program
}
Single line comments are useful for providing explanations, annotations, or for disabling certain lines of code during debugging.
// Below is a temporary disable of x
// int x = 5;
int y = 10;
It's a good practice to use comments to make your code more readable and to explain the purpose of complex logic or behavior within the code.
// Function to calculate the factorial
int factorial(int n) {
if (n <= 1) return 1; // Base case
return n * factorial(n - 1); // Recursive call
}