C Comments
Master the use of comments in C to write clear, maintainable, and readable code.
Comments in C are non-executable lines that explain your code. They help programmers and readers understand logic, debug issues, and document code, but are ignored by the compiler and do not affect program output or performance.
- Improve code readability.
- Help others (and yourself) understand logic later.
- Make debugging easier.
- Temporarily disable code during testing.
- Useful for documentation in large projects.
There are two main types:
- Single-line Comments (
//) - Multi-line Comments (
/* */)
Single-line comments start with // and continue until the end of the line.
Syntax:
c// This is a comment
Example 1: Comment Before Code
c#include <stdio.h> int main() { // This line prints Hello World on screen printf("Hello World!"); return 0; }
textHello World!
Example 2: Comment at End of Line
c#include <stdio.h> int main() { printf("Hello World!"); // Printing message return 0; }
Use this when you want to explain a specific line.
Example 3: Disable Code Using Comments
c#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 a comment.
Multi-line comments are used for longer explanations or to comment out blocks of code over multiple lines.
Syntax:
c/* This is a multi-line comment */
Example 1: Block Description
c#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
c#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.
| 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 |
- Keep comments short and meaningful.
- Avoid obvious comments, such as:
cint a = 10; // Assign 10 to a (not useful)
- Write helpful comments:
cint a = 10; // Initial value for score
- Remove unnecessary comments before final code.
- Update comments when code changes.
Before the C99 standard, C supported only multi-line comments (/* */). Single-line comments (//) were introduced later with C99.
- Comments are used to explain code for human readers and are ignored by the compiler.
- C supports single-line (
//) and multi-line (/* */) comments. - Use comments to improve code readability and maintainability.
- Avoid unnecessary or obvious comments; favor meaningful explanations.
- Always update comments to reflect code changes accurately.
- Add a single-line comment to explain what
printf("Hello");does in a simple C program. - Write a C block that uses multi-line comments to describe its purpose.
- Comment out one line in a three-line function and observe the output.
- Convert an obvious comment into a more meaningful one for clarity.
- Remove unnecessary comments from a sample C program segment.
- Keep comments relevant by updating them when modifying code.
- Avoid stating the obvious and focus on explaining complex logic.
- Use single-line comments for inline explanations and multi-line for block documentation.
- Remove redundant comments before releasing your code.
- Use comments to temporarily disable code during testing, but remember to remove them later.
Quick recap quiz?
We'll generate 5 MCQs from this lesson and check your understanding instantly. Takes ~30 seconds.
Program
#include <stdio.h>
int main(void) {
// Print a greeting message
printf("Hello, World!\n");
/*
The next line is commented out and won't execute
printf("This line is disabled by a multi-line comment\n");
*/
printf("Comments make code clear.\n");
return 0;
}