Comments in C++
Comments in C++ are special notes that we write inside a program to explain the code. They make the program easier to read and understand, especially when someone reads it later. The compiler completely ignores comments, so they never affect the program’s output.
Why Use Comments?
- Improve Readability: Make the code clear and understandable for others or yourself.
- Explain Logic: Helpful in describing what a part of the code is doing.
- Temporarily Disable Code: Used for testing by stopping some lines from running without deleting them.
- Documentation: Provide extra details about a program, function, or block of code.
- Debugging: Comments help in error-finding by turning off some lines while keeping the original code safe.
Types of Comments
-
Single-Line Comment
In C++, a single-line comment begins with
//. Anything written after//on that line is ignored by the compiler. It is used for writing short notes or explaining one line of code.Example:
// This is a single-line comment #include <iostream> using namespace std; int main() { cout << "Hello, From ShikshaSanchar!" << endl; // Prints message return 0; }Output:
Hello, From ShikshaSanchar!
Explanation:
// This is a single-line commentis ignored by the compiler.- The comment after the
coutstatement explains what that line does. - These are best for short and quick explanations in code.
-
Multi-Line Comment
A multi-line comment starts with
/*and ends with*/. Everything between these symbols is ignored by the compiler. They are useful for detailed notes or stopping multiple lines of code from running.Example:
/* This is a multi-line comment It can cover many lines and is ignored by the compiler */ #include <iostream> using namespace std; int main() { cout << "Welcome to ShikshaSanchar C++ Course!" << endl; return 0; }Output:
Welcome to ShikshaSanchar C++ Course!!
Explanation:
- Text between
/*and*/is ignored completely. - They are useful when we want to write bigger notes or disable a block of code.
- Text between
Best Practices for Writing Comments
- Write short and meaningful comments.
- Avoid writing unnecessary comments (like
// prints helloabovecout<<"hello";). - Update comments whenever code is changed.
- Use comments to explain why something is written, not only what it does.
- Prefer single-line comments, use multi-line only when required.
Example with Comments:
In this program, two predefined marks are added together. Comments are written to explain each step clearly.
Program:
// Program to calculate total marks
#include <iostream>
using namespace std;
int main() {
int marks1 = 50; // marks in first subject
int marks2 = 40; // marks in second subject
// Adding the marks
int total = marks1 + marks2;
// Printing the result
cout << "The total marks are: " << total << endl;
return 0;
}
Output:
The total marks are: 90
Explanation:
- marks1 and marks2 store the subject marks.
- The + operator adds the two marks and saves it in total.
- The cout statement displays the total marks on the screen.