C-Program
Comments in c language
Last Updated on: 3rd Jul 2025 15:44:20 PM
Comments in C are used to explain the code, make it more readable, and temporarily disable code during testing or debugging. Comments are ignored by the compiler, so they do not affect the execution of the program.
1. Why Use Comments?
-
To describe the purpose of code sections.
-
To improve code readability for yourself or other programmers.
-
To debug code by disabling specific lines temporarily.
-
To leave notes or reminders for future updates.
2. Types of Comments in C
C supports two types of comments:
a. Single-Line Comments
Starts with //
and continues to the end of the line.
Syntax:
// This is a single-line comment
Example:
#include <stdio.h>
int main() {
int x = 10; // Declare a variable
printf("%d", x); // Print the value
return 0;
}
Explanation:
-
Everything after
//
on that line is ignored by the compiler.
b. Multi-Line Comments
Starts with /*
and ends with */
. It can span across multiple lines.
Syntax:
/*
This is a multi-line comment
that can stretch over several lines
*/
Example:
#include <stdio.h>
int main() {
/* This program prints a number
stored in a variable */
int num = 25;
printf("%d", num);
return 0;
}
Explanation:
-
Useful for longer explanations or commenting out blocks of code.
3. Using Comments to Disable Code
You can use comments to temporarily remove code from execution without deleting it.
Example:
#include <stdio.h>
int main() {
int x = 5;
// x = x + 10; // Temporarily disabled for testing
printf("%d", x);
return 0;
}
-
Use comments to explain “why”, not just “what”.
-
Keep comments short and meaningful.
-
Avoid over-commenting obvious code (e.g.,
i++; // increment i
). -
Update or remove outdated comments during code changes.