Comments in C are non-executable parts of the code used to explain, clarify, or annotate the program.
Thank you for reading this post, don't forget to subscribe!- They are ignored by the compiler and do not affect the program’s execution.
Types of Comments in C:
1.) Single-line Comments:
- Start with //
- Comment lasts until the end of the line.
- Introduced in C99 standard.
Example:
int x = 10; // This is a single-line comment2.) Multi-line Comments:
- Start with
/*and end with*/ - Can span multiple lines.
- Supported since early C standards.
Example:
/*
This is a multi-line comment.
It can span several lines.
*/
int y = 20;Purpose of Comments:
- To explain the purpose of code blocks or logic.
- To make code easier to understand and maintain.
- To temporarily disable code during debugging.
- To add documentation.
Rules and Best Practices:
- Comments should be clear and concise.
- Avoid over-commenting trivial code.
- Do not nest multi-line comments (i.e., no /* … /* … */ … */).
- Use comments to explain why something is done, not what (which should be clear from code).