C Comments
1. What are Comments?
-
Comments are notes written in the code that are ignored by the compiler.
-
They are used to explain code, make it readable, or temporarily disable code.
-
Comments do not affect the program execution.
2. Types of Comments in C
C supports two types of comments:
(a) Single-line Comment
-
Starts with
// -
Everything after
//on that line is ignored by the compiler.
Example:
Output:
The comment
// This is the age of the useris ignored by the compiler.
(b) Multi-line Comment
-
Starts with
/*and ends with*/ -
Can span multiple lines.
Example:
Output:
3. Uses of Comments
-
Explain code for yourself or others.
-
Temporarily disable code for testing:
-
Document complex logic or algorithms.
4. Best Practices
-
Write clear, concise comments.
-
Avoid obvious comments like
int x = 10; // assign 10 to x(the code is self-explanatory). -
Use comments to explain why, not what.
5. Quick Example Combining Both
Output:
