C — Comments
Single-line comments
Use // for a comment on one line:
#include <stdio.h>
int main() {
int x = 10; // This is an assignment
printf("%d\n", x); // Print the value
return 0;
}
Single-line comments are the most common form. Use them to explain why a line exists rather than what it does.
Multi-line comments
Wrap longer comments between /* and */:
#include <stdio.h>
int main() {
/*
This block explains the algorithm
we are about to implement. Multi-line
comments are useful for longer notes.
*/
int result = 42;
printf("%d\n", result);
return 0;
}
Multi-line comments can span any number of lines. They are not nested — opening a second /* inside an active block causes an error:
/*
Valid comment
/* Invalid nested comment — compiler error */
*/
Inline comments
Place a comment at the end of a code line:
#include <stdio.h>
int main() {
int radius = 5;
double area = 3.14159 * radius * radius; // Calculate circle area
printf("Area: %.2f\n", area);
return 0;
}
Commenting out code
Use comments to temporarily disable code during debugging:
#include <stdio.h>
int main() {
int x = 10;
int y = 20;
// printf("Debug: x=%d, y=%d\n", x, y); // Disabled for testing
printf("Sum: %d\n", x + y);
/* This entire block is commented out:
for (int i = 0; i < 10; i++) {
printf("%d ", i);
}
*/
return 0;
}
Documentation comments
Use consistent comment styles for API documentation:
/*
* calculate_sum - Add two integers together.
* @a: First operand.
* @b: Second operand.
*
* Return: The sum of @a and @b.
*
* This is a Linux kernel style documentation comment.
* Tools like KernelDoc can parse these into documentation.
*/
int calculate_sum(int a, int b) {
return a + b;
}
When to comment
Follow these guidelines:
#include <stdio.h>
// GOOD: Explain WHY, not WHAT
int mask = value & 0xFF; // Extract low byte for legacy compatibility
// GOOD: Mark TODOs and FIXMEs
// TODO: Replace with dynamic allocation after sprint 3
// FIXME: This breaks when input is NULL
char buffer[256];
// BAD: Redundant comment
int count = 0; // Initialize count to 0
// GOOD: Explain complex logic
double average = (double)sum / count; // Cast to avoid integer division
Best practices
| Practice | Example |
|---|---|
| Keep comments near code | Comment above the line it explains |
| Update comments with code | Stale comments mislead readers |
| Use TODO/FIXME markers | // TODO: ... is searchable |
| Don't comment obvious code | i++; // increment i is noise |
| Use function-level docs | Document every public function |
Mini Practice
Write C code that:
- Uses
//comments to explain variable declarations - Uses
/* */to write a multi-line description of a function - Comments out a line of code and explains why it's disabled
- Documents a function with parameter descriptions
Up Next
In the next lesson, you'll learn about Variables — declaring and using variables in C.
Related Topics
Frequently Asked Questions about Comments
What is Comments in C?
Comments is a fundamental concept in C. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Comments?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Comments.
Why is Comments important in C?
Comments is essential for C development. Understanding this concept will help you write better code and solve real-world problems more effectively.