-
Hajipur, Bihar, 844101
Writing code is only half the job in programming. The other half is making sure your code is understandable to others—and to yourself when you revisit it later. This is where comments come in. Comments are notes in your program that explain what the code does. They are ignored by the compiler, so they don’t affect how your program runs.
Understanding and using comments effectively is an essential part of writing clean, professional, and maintainable C programs.
Comments in C are lines of text that are included in the source code for explanation purposes. They are meant for human readers, not the computer. The compiler skips them entirely.
In simple terms, comments are instructions or notes that help programmers understand the code’s purpose, logic, or flow without affecting the execution of the program.
Comments are crucial in programming for several reasons:
Code Readability:
They make complex programs easier to read and understand.
Documentation:
Comments can describe what a particular function, block, or line of code does.
Debugging Aid:
You can comment out parts of your code temporarily while testing or debugging without deleting them.
Collaboration:
When multiple programmers work on the same project, comments explain the logic to others.
Future Reference:
If you revisit your code after months or years, comments remind you why certain decisions were made.
C provides two types of comments:
Single-line comments start with //. Everything following // on the same line is treated as a comment.
Example:
#include <stdio.h>
int main() {
int age = 20; // This variable stores the age of the user
printf("Age: %d", age);
return 0;
}
In this example, the compiler ignores the text This variable stores the age of the user.
Multi-line comments start with /* and end with */. They can span multiple lines.
Example:
#include <stdio.h>
int main() {
/* This program demonstrates multi-line comments
It declares a variable, assigns a value,
and prints the value on the screen */
int number = 10;
printf("Number: %d", number);
return 0;
}
This type of comment is useful for detailed explanations or documenting large sections of code.
To use comments effectively, follow these rules:
Do not nest multi-line comments:
Nested /* ... */ inside another multi-line comment can cause errors.
Keep comments clear and concise:
Avoid writing long paragraphs unless necessary. Stick to the point.
Explain ‘why’ not ‘what’ always:
Instead of explaining obvious things (like int a = 5; // declare a variable), explain why you wrote the code.
Avoid over-commenting:
Too many comments can clutter the code and reduce readability.
#include <stdio.h>
int main() {
// Store the user's age
int age = 25;
// Check if the user is eligible to vote
if (age >= 18) {
printf("Eligible to vote\n");
} else {
printf("Not eligible to vote\n");
}
return 0;
}
In this example, comments explain the purpose of the variables and the logic of the condition without stating the obvious.
Comments are often used to temporarily disable parts of the code during testing or debugging. This is called commenting out code.
Example:
#include <stdio.h>
int main() {
int x = 10;
int y = 20;
// int sum = x + y; // This line is ignored
printf("Value of x: %d\n", x);
printf("Value of y: %d\n", y);
return 0;
}
Here, the sum calculation is temporarily disabled. You can easily re-enable it later by removing the //.
Use meaningful comments:
Instead of stating the obvious, explain the logic or intention behind the code.
Keep comments up-to-date:
Outdated comments can confuse others more than having no comments at all.
Use multi-line comments for documentation:
Use /* ... */ for functions, algorithms, or important sections of code.
Use single-line comments for inline explanations:
Use // for short explanations next to specific lines of code.
#include <stdio.h>
int main() {
/* Program to calculate the area of a rectangle
Steps:
1. Declare length and width
2. Calculate area = length * width
3. Display the result
*/
int length = 10; // Length of rectangle
int width = 5; // Width of rectangle
int area = length * width; // Area calculation
printf("Area of rectangle: %d\n", area); // Display result
return 0;
}
Output:
Area of rectangle: 50
This program uses both single-line and multi-line comments to explain its logic and flow clearly.
Comments are an essential part of writing professional C programs. They improve code readability, provide documentation, help in debugging, and facilitate collaboration. By using single-line and multi-line comments effectively, you can make your programs easier to understand for yourself and others.
Remember, good comments explain the intention behind the code, not just what the code does. Over time, developing the habit of writing meaningful comments will make your programming skills much stronger.
What is the purpose of comments in C programming?
What are the two types of comments in C, and how are they written?
Explain why C ignores comments during program execution.
Identify the error in the following code:
/* This is a multi-line comment
/* Nested comment example */
printf("Hello");
Write a single-line comment explaining the purpose of a variable named totalMarks.
How can comments be used for debugging or temporarily disabling code?
Write a small C program with a multi-line comment explaining the steps of a calculation.
Why is it better to explain “why” in comments rather than “what”?
Give an example of a meaningful single-line comment and a meaningful multi-line comment.
What are some best practices for writing comments in professional C programs?