C Programming Comments
Comments in C are used to explain code.
They are ignored by the compiler, which means they do not affect program execution.
Comments make programs:
- Easy to understand
- Easy to read
- Easy to maintain
Why Comments Are Used
- To explain what the code does
- To make code readable for others
- To remember logic when revisiting code
- To temporarily disable code during testing
Types of Comments in C
C supports two types of comments:
- Single-line comment
- Multi-line comment
1. Single-Line Comment
A single-line comment starts with //
Everything after // on that line is treated as a comment.
Syntax
// This is a single-line commentExample
int age = 20; // storing ageHere:
// storing age is ignored by the compiler
2. Multi-Line Comment
A multi-line comment starts with /* and ends with */
It can span across multiple lines.
Syntax
/* This is a
multi-line comment */Example
/* This program
prints Hello World */
printf("Hello World");Commenting Out Code (Very Common Use)
Comments are often used to disable code temporarily.
// printf("This line will not execute");or
/*
printf("Line 1");
printf("Line 2");
*/Important Rules About Comments
- Comments are not executed
- Nested comments are not allowed
- Comments cannot be used inside strings
Wrong:
printf("Hello // world");Difference Between Single-Line and Multi-Line Comments
| Feature | Single-Line | Multi-Line |
| Symbol | // | /* */ |
| Lines | One line only | Multiple lines |
| Usage | Short explanation | Detailed explanation |
One-Line Exam Answer
Comments in C are used to explain the code and are ignored by the compiler during program execution.
