C Comments
3 min read ·
Comments are used to explain the code and make it easier to understand. They are ignored by the compiler, which means they do not affect how the program runs.
Comments are very important in programming because they help you and others understand the logic of the code, especially in large projects.
Note
Comments are only for humans, not for the computer. The compiler completely ignores them during execution.
Single Line Comments
A single line comment starts with double forward slashes. It is used to write short explanations.
Explanation
The text after // is treated as a comment
It is used for quick notes or explanations
You can also place comments at the end of a line.
Pro Tip
Use single line comments for short and clear explanations of code.
Multi Line Comments
Multi line comments are used when you need to write longer explanations. They start with /* and end with */.
Explanation
Everything between /* and */ is treated as a comment
Useful for describing logic or sections of code
When to Use Comments
Comments should be used to improve readability and explain complex logic.
You can use comments to describe what a program does
Explain difficult parts of code
Add notes for future improvements
Real World Scenario
In real projects, developers use comments to explain functions and logic so that other team members can understand and maintain the code easily.
Good vs Bad Comments
Good Comment Example
Bad Comment Example
Explanation
Good comments explain why something is done
Bad comments repeat what is already obvious
Caution
Do not overuse comments. Writing too many unnecessary comments can make the code harder to read.
Commenting Out Code
Comments can also be used to temporarily disable code during testing.
Explanation
The commented line is ignored during execution
Useful for debugging
Goal Achieved
You now understand how to use comments in C to improve code readability and maintainability.
Exercise
Write a C program and add comments to explain each line of the code.