Turning Learners into Developers
- Comments in C Programming
Comments in C are non-executable lines used to explain code. They are written for humans, not for the compiler, making programs easier to understand, debug, and maintain.
In simple words:
- Comments = Explanation inside your code
- Why Use Comments in C?
- Improve code readability
- Help others (and yourself) understand logic later
- Make debugging easier
- Temporarily disable code during testing
- Useful for documentation in large projects
Important Note
- Comments are ignored by the compiler
- They do NOT affect program output or performance
- Types of Comments in C
There are two types of comments in C:
- Single-line Comments (//)
- Multi-line Comments (/* */)
 1. Single-line Comments (//)
- Single-line comments start with // and continue until the end of the line.
✅ Syntax:
// This is a commentExample 1: Comment Before Code
#include <stdio.h>
int main() {
// This line prints Hello World on screen
printf("Hello World!");
return 0;
}Output:
Hello World!
Example 2: Comment at End of Line
#include <stdio.h>
int main() {
printf("Hello World!"); // Printing message
return 0;
}Useful when you want to explain a specific line.
Example 3: Disable Code Using Comments
#include <stdio.h>
int main() {
printf("This will run\n");
// printf("This will NOT run\n");
return 0;
}Here, the second printf is disabled using comments.
2. Multi-line Comments (/* */)
- Multi-line comments are used for long explanations or commenting multiple lines.
✅ Syntax:
/* This is a multi-line comment */Example 1: Block Description
#include <stdio.h>
int main() {
/*
This program prints Hello World
It is a basic C program example
*/
printf("Hello World!");
return 0;
}Example 2: Comment Multiple Lines of Code
#include <stdio.h>
int main() {
/*
printf("Line 1\n");
printf("Line 2\n");
*/
printf("Only this line will execute\n");
return 0;
}Â Both printf statements inside the comment are ignored.
- Single-line vs Multi-line Comments
| Feature | Single-line ( | Multi-line ( |
|---|---|---|
Usage | Short notes | Long explanations |
Lines Supported | One line | Multiple lines |
Readability | Quick comments | Detailed description |
Common Use | Inline comments | Block comments |
- Best Practices for Using Comments
- Keep comments short and meaningful
- Avoid obvious comments like:
int a = 10; // Assign 10 to a (not useful)- Write helpful comments:
int a = 10; // Initial value for score- Remove unnecessary comments before final code
- Update comments when code changes
Historical Note
- Before C99 standard release, C supported only multi-line comments (/* */).
- Single-line comments (//) were introduced later in C99.
01
C Introduction
02
C Get Started
03
C Syntax
04
C Input & Output
05
C Comments
06
C Variables
07
C Data Types
08
C Constants
09
C Operators
10
C Booleans
11
C Type Conversion
12
C If...Else
13
C Switch
14
C For Loop
15
C While Loop
16
C Do/While Loop
17
C Nested For Loop
18
C Break/Continue
19
C Arrays
20
C Strings
21
C User Input
22
C Memory Address
23
C Pointers
24
C Functions
25
C Files
26
C Structures
27
C Enums
28
C Memory
29
C Errors
30
C More
31
C Projects
32
C Reference
33
C Examples
34
DS With C
35
C Exam Questions
36