C Comments
C comments are notes written inside source code for human readers. They explain intent, temporarily mark information, and make programs easier to maintain without changing what the program does. The compiler removes or ignores comments before normal C statements are translated, so good comments help people while leaving the executable behavior unchanged.
Overview: How C Comments Work
A comment is part of the source file, but it is not part of the running program. When the C translation process begins, comments are replaced by whitespace before the compiler performs its main grammar checks. That means comments do not create variables, call functions, allocate memory, or execute instructions. They are useful because source code is read many more times than it is written, often by someone who was not present when the code was first created.
C has two comment forms. A single-line comment starts with // and continues to the end of the current line. A block comment starts with /* and ends at the next */. Block comments can span several lines, but they do not nest in standard C. If you start one block comment inside another, the first */ still ends the whole comment, which can leave unexpected text for the compiler to parse.
Comments are especially helpful for explaining why code exists, what assumptions it depends on, and what a non-obvious calculation means. They are less helpful when they merely repeat what the syntax already says. For example, count++; usually does not need a comment saying that it increases count. A better comment might explain why this particular count is being increased at that point in the program.
Because comments are removed before normal compilation, they cannot be used to make invalid C valid, except by hiding the invalid text completely. Comments also do not work inside string literals. If you write "// not a comment", those characters are part of the string data printed or stored by the program. The compiler recognizes strings and comments according to lexical rules, so the location of the markers matters.
Syntax
The two comment forms look like this in a complete program:
#include <stdio.h>
int main(void)
{
// A single-line comment ends at the newline.
printf("Single-line comments are useful.\n");
/* A block comment can describe
a small group of related statements. */
printf("Block comments are useful too.\n");
return 0;
}
Output:
Single-line comments are useful.
Block comments are useful too.
| Form | Starts With | Ends When | Best Use |
|---|---|---|---|
| Single-line comment | // |
The line ends | Short notes beside or above one statement |
| Block comment | /* |
The next */ appears |
Longer notes, file headers, or grouped explanations |
- Use
//for quick explanations that fit on one line. - Use
/* ... */when the note naturally spans multiple lines. - Do not place comments where they split a keyword, identifier, number, or operator.
- Remember that comments inside strings are not comments; they are ordinary characters.
Examples
Example 1: Explaining Intent
#include <stdio.h>
int main(void)
{
int minutes = 135;
// Convert total minutes into hours and remaining minutes.
int hours = minutes / 60;
int remaining = minutes % 60;
printf("Total minutes: %d\n", minutes);
printf("Hours: %d\n", hours);
printf("Remaining minutes: %d\n", remaining);
return 0;
}
Output:
Total minutes: 135
Hours: 2
Remaining minutes: 15
The comment explains the purpose of the next two calculations. It does not repeat every operator. A reader can see the division and remainder operations, but the comment tells them the larger idea: converting one unit into two related values.
Example 2: Using A Block Comment For A Group
#include <stdio.h>
int main(void)
{
double price = 24.00;
double tax_rate = 0.075;
/* The final cost is the base price plus sales tax.
Keep the tax calculation separate so it can be printed. */
double tax = price * tax_rate;
double total = price + tax;
printf("Price: $%.2f\n", price);
printf("Tax: $%.2f\n", tax);
printf("Total: $%.2f\n", total);
return 0;
}
Output:
Price: $24.00
Tax: $1.80
Total: $25.80
A block comment works well here because the note covers a small group of related statements. The program still performs the arithmetic normally. The comment is removed before the compiled program runs, so it has no effect on the floating-point values or the printed output.
Example 3: Comment Markers Inside Strings
#include <stdio.h>
int main(void)
{
printf("This text contains // but it is still printed.\n");
printf("This text contains /* and */ too.\n");
return 0;
}
Output:
This text contains // but it is still printed.
This text contains /* and */ too.
The comment markers are inside string literals, so they are not treated as comments. They become characters in the program’s data and are printed by printf. This distinction matters when reading code that prints examples, URLs, patterns, or documentation text.
Example 4: Temporarily Disabling One Statement
#include <stdio.h>
int main(void)
{
printf("Normal report\n");
// printf("Debug detail: reached checkpoint A\n");
printf("Report complete\n");
return 0;
}
Output:
Normal report
Report complete
A single-line comment can temporarily disable a statement while testing. This technique is useful for short experiments, but it should not become a permanent way to manage features. Finished programs should remove stale commented-out code or replace it with a real option, condition, or logging setting.
How It Works Step By Step
- The source file is read during the early translation phases.
- The compiler recognizes comments outside string and character literals.
- Each comment is replaced as if it were whitespace, preserving separation between nearby tokens.
- Preprocessing directives such as
#include <stdio.h>are handled. - The compiler then parses declarations, statements, expressions, and types from the remaining tokens.
- No machine instructions are generated for comments, so comments do not affect runtime speed or memory use.
There is one practical consequence: comments can separate tokens, but they should not be used in clever ways. For example, writing pieces of a statement with comments inserted between them may compile, but it makes code harder to read. Treat comments as documentation, not as punctuation tricks.
Common Mistakes
Trying To Nest Block Comments
Block comments end at the first */. If you put a block comment inside another block comment, the outer comment does not wait for a second ending marker. The reliable fix is to avoid nesting and use single-line comments for temporary disabling when needed.
#include <stdio.h>
int main(void)
{
// printf("First disabled line\n");
// printf("Second disabled line\n");
printf("Only the active line runs\n");
return 0;
}
Output:
Only the active line runs
Letting Comments Become Outdated
An incorrect comment is worse than no comment because it confidently points the reader in the wrong direction. When the code changes, update the comment at the same time.
#include <stdio.h>
int main(void)
{
int length = 8;
int width = 5;
// Calculate the rectangle area.
int area = length * width;
printf("Area: %d\n", area);
return 0;
}
Output:
Area: 40
The comment matches the calculation. If the formula later changed to perimeter, the comment would need to change too. Accurate comments are part of maintaining correct code.
Using Comments Instead Of Clear Names
Comments cannot rescue consistently confusing names. A variable named x might be acceptable in a tiny formula, but names such as minutes, tax_rate, and remaining often remove the need for extra explanation. Prefer clear code first, then add comments for context that the code itself cannot express.
Best Practices
- Explain why something is done, not only what the syntax does.
- Place comments close to the code they describe.
- Keep comments accurate when you edit the related code.
- Use complete sentences for longer notes so they are easy to read later.
- Do not leave large blocks of old commented-out code in finished programs.
- Avoid decorative comment boxes that are harder to maintain than the text inside them.
- Use comments to document assumptions, units, formulas, limits, and surprising decisions.
- Let good names and simple structure reduce the number of comments you need.
Practice Exercises
- Write a program that converts centimeters to meters. Add one useful comment explaining the conversion formula.
- Create a program that prints a short receipt with price, tax, and total. Use a block comment to describe the calculation group.
- Write a program that prints the characters
//,/*, and*/as text. Predict why they are not treated as comments.
Summary
- C supports single-line comments with
//and block comments with/* ... */. - Comments are removed or ignored during translation and produce no runtime instructions.
- Comment markers inside string literals are printed or stored as ordinary characters.
- Block comments do not nest, so use them carefully around existing comments.
- The best comments explain intent, assumptions, and non-obvious decisions.
- Clear names and simple code reduce the need for excessive commenting.
