Topic 8 of 100 Fundamentals of C Interactive Tutorial

Comments in C: Syntax, Styles, and Practical Usage

8 mins read Last updated: 22 Aug 2026 15,400 views
C Compiler 0.00%

What is Comments in C? Core Concept & Direct Summary

Core Objective & Problem Statement

How are single-line and multi-line comments used in C, and how does the preprocessor process them?

Direct Answer Summary

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:

main.c
Run Online
#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
}

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:

main.c
Run Online
/*
 * Program:  Temperature Converter
 * Author:   Your Name
 * Date:     September 2026
 * Purpose:  Converts Celsius to Fahrenheit
 *           using the formula F = C * 9/5 + 32
 */
#include <stdio.h>
int main() {
    float celsius = 25.0;
    float fahrenheit = celsius * 9 / 5 + 32;
    /* apply the formula */
    printf("%.1f°C = %.1f°F\n", celsius, fahrenheit);
    return 0;
}

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.

main.c
Run Online
/*   Trying to explain the outer block...
   /* inner comment attempt */ ← the comment ENDS here!
This text is now CODE. The compiler tries to compile it. 💥*/

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:

main.c
Run Online
#include <stdio.h>
int main() {
    printf("This runs.\n");
    #if 0 printf("This never compiles.\n");
    /* Even block comments inside are totally fine here */ int broken =;
    // even invalid code is ignored#endif
    printf("Back to normal.\n");
    return 0;
}

#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...

SituationExample
Whysomething exists// delay needed — sensor needs 2ms to stabilize
Non-obvious tricks// XOR swap: exchanges values without a temp variable
Assumptions and limits// assumes input is always positive
Section markers/* ---------- Input Handling ---------- */
Warnings// DON'T call this before initDisplay()

❌ DON'T comment...

main.c
Run Online
int i = 0;
// declaring i as 0        ← useless, code already says it
i = i + 1;
// adding 1 to i           ← pure noise
printf("Hi\n");
// printing Hi             ← really?

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.

main.c
Run Online
// ❌ check if the year is divisible by 4 but not by 100, unless by 400
if (y % 4 == 0 &&(y % 100 != 0 || y % 400 == 0)) {
    ...
}
// ✅ the name explains it — no comment needed
int isLeapYear =(y % 4 == 0 &&(y % 100 != 0 || y % 400 == 0));
if (isLeapYear) {
    ...
}

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:

main.c
Run Online
/*
 * File:        grade_calculator.c
 * Author:      Your Name
 * Created:     2026-09-21
 * Description: Reads student marks and prints letter grades.
 *              Uses standard 90/80/70/60 grade boundaries.
 * Compile:     gcc grade_calculator.c -o grades
 */

Thirty seconds of writing, hours of future confusion prevented.

Runnable C Code Example & Practical Implementation

main.c
Run Online
#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

Best Practice & Key Takeaways
Use #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 questions
Q

Q: Can you nest `/* ... */` comments in standard C?

A: No. The compiler matches the first `/*` with the very next `*/` it encounters, causing subsequent code or the second closing `*/` to trigger a syntax error.
Q

Q: How do you temporarily disable a block of code containing comments?

A: Use preprocessor directives: `#if 0 ... #endif`.

Related Tools, Tutorials & Authoritative References

Recommended Interactive Tools

Authoritative Standards & Citations