#include <stdio.h>
int main() {
int score = 95;
// store the player's score
// Print the result with a friendly message
printf("Score: %d\n", score);
return 0;
// 0 means success
}Comments in C: Syntax, Styles, and Practical Usage
What is Comments in C? Core Concept & Direct Summary
How are single-line and multi-line comments used in C, and how does the preprocessor process them?
Here's a confession every experienced programmer will make:we don't write comments for the computer. We write them for the exhausted human reading the code at 2 AM — who is usually ourselves, three months later.
Comments are the most misunderstood feature in programming. Beginners either skip them entirely ("the code explains itself!") or plaster them on every line like wallpaper. Both approaches miss the point.
In this guide, you'll learn exactly how comments work in C — both syntax styles, the sneaky nesting trap that has bitten generations of programmers, and the professional judgment aboutwhena comment helps and when it's just noise. Short topic, big payoff.
How Does Comments in C Work? In-Depth Mechanics & Architecture
What Are Comments in C?
Acommentis text inside your source code that the compiler completely ignores. When C compiles your program, every comment is stripped away — they cost zero memory, zero speed, zero anything at runtime.
Their entire purpose is communication:
- Explainwhya piece of code exists
- Warn aboutnon-obviousbehavior
- Mark sections so readers can navigate
- Temporarilydisable codeduring testing
Think of comments as the margin notes in a textbook — the text works without them, but the notes make itunderstandable.
Style #1: Single-Line Comments (//)
Introduced in C99 (borrowed from C++), single-line comments start with//and run to the end of the line:
Everything after//on that line is invisible to the compiler. Place themabovethe code they describe, orat the endof the line for short notes — both placements are standard practice.
Style #2: Multi-Line Comments (/* */)
The classic C comment wraps text between/*and*/— and can stretch across as many lines as you need:
Two placement styles, both valid:
- Block style— the decorative
/* ... */header at the top of files (the leading*on each line is pure decoration, not required) - Inline style—
/* like this */sitting mid-line or after a statement
⚠️ The Nested Comment Trap (Read This Before It Bites You)
Here's the gotcha that has confused C programmers for fifty years:multi-line comments cannot be nested.
Why? The compiler matches/*with thevery next*/it finds — it doesn't count nesting levels. The moment it hits that inner*/, the comment closes, and everything after gets compiled as code, producing errors that baffle beginners.
The safe rule:one/*pairs with the next*/, period. If you need to comment out a block that already contains/* */comments, use single-line comments instead — or better, the preprocessor trick below.
Pro Trick: Commenting Out Code With #if 0
When debugging, you often want to disable a whole chunk of code temporarily. Wrapping it in/* */fails if the chunk contains comments (the nesting trap). The professional solution:
#if 0tells the preprocessor "this condition is false — skip everything until#endif." It handles nesting gracefully and flips back on by changing0to1. Old-school, battle-tested, everywhere in real codebases.
When to Comment (and When Not To)
Syntax is easy.Judgmentis the real skill. Here's the framework professionals use:
✅ DO comment...
❌ DON'T comment...
The golden rule:good comments explainwhy; bad comments narratewhat.If the code clearly shows what it does, a comment repeating it just creates a second thing to maintain — and worse, comments can go stale. A comment that says the opposite of what the code does is worse than no comment at all.
[IMAGE 3 — Infographic: "Why vs What" Comment Guide]Design:Two columns — green "WHY ✅" side with good comment examples, red "WHAT ❌" side with narration examples. Headline: "Comment the why, not the what."Alt text:
Best practices for comments in C explain why not what with examples
Comments vs Self-Documenting Code
One more professional secret: the best comment is oftenbetter code.
Descriptive variable and function names eliminate whole categories of comments. When you feel the urge to write a long comment explaining a confusing line, pause and ask:could I rename something instead?
A Real-World Header Template
Copy this for your own programs — it follows conventions used in professional C projects:
Thirty seconds of writing, hours of future confusion prevented.
Runnable C Code Example & Practical Implementation
#include <stdio.h>
/*
* Multi-line header comment:
* Demonstration of comments in C.
*/
int main() {
// Single-line comment explaining variable initialization
int bufferSize = 1024;
// Size in bytes
/* Block comment inside code */
printf("Buffer size: %d bytes\
", bufferSize);
return 0;
}Key Takeaways, Best Practices & Memory Safety Rules
#if 0 ... #endif to comment out large chunks of debugging code containing inner comments safely.Frequently Asked Questions & Technical Interview Insights
Technical Interview & Viva Questions
Frequently asked C programming interview questionsQ: Can you nest `/* ... */` comments in standard C?
Q: How do you temporarily disable a block of code containing comments?
Related Tools, Tutorials & Authoritative References
Recommended Interactive Tools
- Online C Compiler & Runner — Interactive online GCC sandbox to execute C code in real time.
- Online C Code Editor & IDE — Multi-file C programming environment with code formatting.
- C++ Programming Tutorial — Transition from procedural C to Object-Oriented C++ with STL.
- JavaScript Programming Tutorial — Master modern JavaScript fundamentals, DOM APIs, and async patterns.
Authoritative Standards & Citations
- ISO/IEC 9899 (C Language Standard) — Official international C specification by ISO/IEC JTC 1/SC 22/WG 14.
- GNU GCC Documentation — Official reference manual for the GNU Compiler Collection and C libraries.
- The C Programming Language (K&R) — Definitive classic work by Brian Kernighan & Dennis Ritchie at Bell Labs.
- C Reference Documentation (cppreference.com) — Verified reference covering C89 through C23 language features.