Comments in C
Comments in C are special notes written inside a program to explain the code. They make the program easier to read and understand. The compiler completely ignores comments, so comments do not affect the output of the program.
Comments are useful for revision, debugging, teamwork and explaining important logic in a program.
Why Use Comments?
- Improve Readability: Make code clear and easy to understand.
- Explain Logic: Describe important steps or formulas.
- Help in Revision: Make old code easier to revise later.
- Temporarily Disable Code: Stop some lines from running while testing.
- Documentation: Explain functions, variables or program purpose.
Types of Comments in C
-
Single-Line Comment
A single-line comment starts with
//. Anything written after//on the same line is ignored by the compiler.Example:
// This is a single-line comment #include <stdio.h> int main() { printf("Hello, From ShikshaSanchar!"); // Prints message return 0; }Output:
Hello, From ShikshaSanchar!
Explanation:
// This is a single-line commentis ignored by the compiler.- The comment after
printf()explains the purpose of that line. - Single-line comments are best for short notes.
-
Multi-Line Comment
A multi-line comment starts with
/*and ends with*/. Everything written between these symbols is ignored by the compiler.Example:
/* This is a multi-line comment It can cover many lines and it is ignored by the compiler */ #include <stdio.h> int main() { printf("Welcome to ShikshaSanchar C Course!"); return 0; }Output:
Welcome to ShikshaSanchar C Course!
Explanation:
- Text between
/*and*/is ignored completely. - Multi-line comments are useful for longer explanations.
- They can also be used to disable multiple lines temporarily.
- Text between
Best Practices for Writing Comments
- Write short and meaningful comments.
- Use comments to explain important logic, not every simple line.
- Update comments whenever code changes.
- Avoid wrong or confusing comments.
- Use comments to explain why something is written.
Example with Comments:
In this program, marks of two subjects are added. Comments are written to explain each important step.
Program:
// Program to calculate total marks
#include <stdio.h>
int main() {
int marks1 = 50; // marks in first subject
int marks2 = 40; // marks in second subject
// Adding both marks
int total = marks1 + marks2;
printf("The total marks are: %d", total);
return 0;
}
Output:
The total marks are: 90
Explanation:
- marks1 and marks2 store the subject marks.
- The + operator adds both marks and stores the result in total.
- The printf() function displays the total marks on the screen.
- Comments make each step easier to understand.
Common Mistakes with Comments
- Forgetting to close a multi-line comment with
*/. - Writing too many unnecessary comments.
- Writing comments that do not match the actual code.
- Trying to nest one multi-line comment inside another multi-line comment.
Summary:
- Comments are notes written inside a C program.
- The compiler ignores comments, so they do not affect output.
- Single-line comments start with
//. - Multi-line comments start with
/*and end with*/. - Good comments improve readability, revision and code understanding.