Comments in C

Comments add explanations for people reading source code. They are not executable C statements. Use // for a comment that continues to the end of the line, or /* ... */ for a block comment.

Single-Line Comments Top ↑

int num = 10;  // Store an integer value

// Display the value
printf("%d\n", num);

Everything after // on that source line is part of the comment.

Block Comments Top ↑

/* This is a block comment.
   It can continue across
   multiple source lines. */

Comments Inside a Working Program Top ↑

#include <stdio.h>

int main(void) {
    double number = 17.873;  // Declare and initialize

    /* Display the value with
       three digits after the decimal point. */
    printf("number = %.3f\n", number);
    return 0;
}

Output

number = 17.873

Avoid Nested Block Comments Top ↑

Standard C block comments do not nest. This should not be used:

/* outer comment /* inner comment */ rest of text */

The first */ ends the block comment, leaving the remaining text to be parsed as source code.

Good Comment Practice Top ↑

  • Explain why a non-obvious step is needed rather than repeating what the code already says.
  • Keep comments synchronized when code changes.
  • Do not use comments to hide large amounts of obsolete code in a finished tutorial example.




plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer