Comments in C are notes written inside the source code for human readers. They help explain logic, describe intent, mark reminders, and make the code easier to understand. The compiler ignores comments, so they do not affect the actual execution of the program.
This topic is simple in syntax but important in practice. Beginners often either avoid comments completely or write too many weak comments that add no real value. In this article, we will learn what comments in C are, the two types of comments, why comments are useful, where they should be used, where they should not be overused, and the best practices that keep code readable and maintainable.
What are Comments in C?
Comments in C are non-executable lines or sections of text placed in the source code to explain something to the programmer. Since the compiler ignores them, comments are used only for documentation, clarification, reminders, and communication between developers.
For example, a comment can explain why a certain logic is used, warn about a tricky section, or describe the purpose of a function. Comments are not instructions for the computer. They are explanations for humans.
Why Comments are Used in C
- They explain the purpose of code.
- They make complex logic easier to understand.
- They help during debugging and maintenance.
- They improve collaboration when multiple people work on the same code.
- They make it easier to revisit old code after a long time.
Well-written comments save time. Poor comments waste attention. That is why good commenting is not about writing more comments, but about writing useful ones.
Types of Comments in C
C supports two main types of comments: single-line comments and multi-line comments.
1. Single-Line Comments
Single-line comments begin with // and continue up to the end of the line. They are useful for short explanations or quick notes near a statement.
// This is a single-line comment.
int total = 0; // initialize the running totalSingle-line comments are common in modern code because they are short, clear, and easy to place beside or above a statement.
2. Multi-Line Comments
Multi-line comments begin with /* and end with */. They can span multiple lines, which makes them useful for longer explanations or temporarily disabling larger sections of code during testing.
/*
This is a multi-line comment.
It can cover more than one line.
*/
int count = 10;Multi-line comments are especially useful when the explanation needs more space than a single line can provide.
Difference Between Single-Line and Multi-Line Comments
| Type | Syntax | Best Used For |
|---|---|---|
| Single-line comment | // comment | Short notes and quick explanations |
| Multi-line comment | /* comment */ | Longer explanations or grouped notes |
Both forms are valid, but the choice depends on readability and intent. The goal should always be clarity.
Do Comments Affect Program Execution?
No. Comments do not affect the compiled program logic. The compiler ignores them while processing the code. That means comments do not change the output, speed, or runtime behavior of the program.
This is why comments are safe to use for explanation. However, they still matter because they affect how humans understand and maintain the code.
Examples of Comments in C
Here is a simple example showing comments used inside a program.
#include <stdio.h>
int main(void)
{
int marks = 85; // store student's marks
/* Check whether the student passed
based on a simple threshold */
if (marks >= 40)
{
printf("Pass\n");
}
return 0;
}In this example, the comments do not change the logic. They simply explain the purpose of certain parts of the code.
When Comments are Helpful
- When the code involves tricky logic
- When you need to explain why a specific approach was chosen
- When a formula or algorithm may confuse a reader
- When a temporary note or TODO needs to be left for future work
- When a function or block has a purpose that is not obvious from the code alone
Comments are most valuable when they explain intent, assumptions, or unusual decisions. Those are the things that may not be obvious just by reading the statements.
When Comments Become Unhelpful
Not every comment improves code. Comments become weak when they only restate the obvious.
| Poor Comment | Why It Is Weak |
|---|---|
// increment i above i++; | The code already says that clearly |
// print value above printf(...); | Adds almost no useful information |
| Long outdated comment | Can mislead the reader if the code changes later |
A good rule is this: if the code already says something clearly, the comment should add context, not repetition.
Can Comments be Used to Disable Code?
Yes, comments are often used temporarily to disable lines or blocks of code during testing or debugging. For example, a programmer may comment out a statement to check whether a bug disappears.
However, this should be used carefully. Leaving large commented-out code blocks in final code can make the file messy and harder to read. If something is no longer needed, it is often better to remove it cleanly or use version control to keep the history.
Can Multi-Line Comments be Nested in C?
No. Standard C does not support nested block comments. That means you should not place /* ... */ inside another /* ... */ comment block, because the compiler will treat the first closing */ as the end of the comment.
This is one reason single-line comments are often safer for temporarily disabling code that may already contain comments inside it.
Best Practices for Comments in C
- Write comments that explain intent, not obvious syntax.
- Keep comments short, clear, and relevant.
- Update comments when code changes.
- Use comments to explain assumptions, formulas, and unusual logic.
- Avoid cluttering the code with repetitive comments.
- Prefer meaningful variable and function names so fewer comments are needed.
Comments work best when they support readable code, not when they are used as a substitute for readable code.
Common Mistakes with Comments in C
- Writing comments for every obvious line
- Leaving outdated comments after changing the code
- Using comments instead of improving bad variable names
- Leaving large dead code blocks commented out for too long
- Writing vague comments that do not really explain anything
These mistakes reduce the value of comments and sometimes make the code harder to maintain than if there were no comments at all.
Comments vs Meaningful Code
One of the best lessons for a beginner is that comments are helpful, but meaningful code is even better. If you choose clear names such as totalMarks, isPassed, and calculateAverage, the need for unnecessary comments becomes smaller.
The strongest codebases usually combine both: clear code and targeted comments where extra explanation is genuinely needed.
FAQs
What are comments in C?
Comments in C are notes written inside source code for human readers. They are ignored by the compiler and do not affect program execution.
What are the types of comments in C?
C supports two main types of comments: single-line comments using // and multi-line comments using /* ... */.
Do comments affect performance in C?
No. Comments do not affect program speed or behavior because the compiler ignores them during compilation.
Can block comments be nested in C?
No. Multi-line block comments cannot be nested in standard C.
Should every line be commented in C?
No. Comments should explain important intent or confusing logic, not repeat what already obvious code clearly shows.