- Single-line comments using
//. - Multi-line comments using
/* ... */.
Syntax of Comments
| Comment Type | Syntax | Used For |
|---|---|---|
| Single-line Comment | // Comment |
Writing a comment on a single line. |
| Multi-line Comment | /* Comment */ |
Writing comments that span multiple lines. |
Example 1: Single-Line Comment
Single-line comments begin with // and continue until the end of the line.
#include
int main()
{
// Display a welcome message
printf("Welcome to C Programming!");
return 0;
}
Output
Welcome to C Programming!
Example 2: Multi-Line Comment
Multi-line comments begin with /* and end with */.
They are useful for longer descriptions or documentation.
#include
int main()
{
/*
This program prints
a welcome message
on the screen.
*/
printf("Hello World!");
return 0;
}
Output
Hello World!
Example 3: Commenting Out Code
Comments can also be used temporarily to disable a piece of code during testing or debugging.
#include
int main()
{
printf("Program Started\n");
/*
printf("This line is disabled.");
*/
printf("Program Finished");
return 0;
}
Output
Program Started
Program Finished
Single-Line vs Multi-Line Comments
| Feature | Single-Line (//) |
Multi-Line (/* ... */) |
|---|---|---|
| Starts With | // |
/* |
| Ends With | End of the current line | */ |
| Can Span Multiple Lines | No | Yes |
| Best Used For | Short explanations | Detailed descriptions and documentation |
Why Are Comments Important?
Benefits
- Improve code readability.
- Explain complex logic.
- Make code easier to maintain.
- Help other programmers understand the code.
- Useful for documentation.
- Can temporarily disable code during debugging.
Common Mistakes
Avoid These Mistakes
- Forgetting to close a multi-line comment with
*/. - Writing nested multi-line comments (not supported in C).
- Using comments instead of meaningful variable and function names.
- Leaving outdated comments after modifying the code.
Best Practices
- Write clear and meaningful comments.
- Keep comments updated when the code changes.
- Use single-line comments for short explanations.
- Use multi-line comments for documentation and detailed descriptions.
- Avoid writing comments that simply repeat what the code already says.
Prerequisites
Before Learning This Topic
- Basic understanding of C program structure.
- Knowledge of C statements and functions.
- Familiarity with writing simple C programs.
Interview Questions
- What are the different types of comments in C?
- What is the syntax for a single-line comment?
- What is the syntax for a multi-line comment?
- Can multi-line comments be nested in C?
- Does the C compiler execute comments?
- Why are comments important in programming?
Key Takeaway
Comments in C are used to explain source code and improve readability.
C supports two types of comments: single-line comments
(//) and multi-line comments
(/* ... */). Since comments are ignored by the compiler,
they do not affect program execution but play an essential role in code
documentation, debugging, and maintenance.