Skip to main content

Comments in C Programming

2 min read Updated May 29, 2025
Share:
On this page (9sections)

Introduction

Comments document your code for other programmers (and your future self). The C compiler ignores comments completely — they do not affect program behaviour.

Single-Line Comments (C99 and later)

Use // for comments that end at the end of the line:

#include <stdio.h>

int main(void) {
    int count = 10;  // number of items
    // printf("debug: count = %d\n", count);
    printf("Count: %d\n", count);
    return 0;
}

Multi-Line Comments (C89 and later)

Use /* to start and */ to end:

/*
 * This program demonstrates comments.
 * Author: Student
 */

#include <stdio.h>

int main(void) {
    printf("Hello\n");  /* print greeting */
    return 0;
}

Example Program

#include <stdio.h>

/* Function to add two integers */
int add(int a, int b) {
    return a + b;  // return sum
}

int main(void) {
    int result = add(5, 3);
    printf("Sum = %d\n", result);
    return 0;
}

Sample Output

Sum = 8

Best Practices

  • Explain why, not whati++ does not need a comment saying “increment i”.
  • Keep comments up to date — outdated comments mislead readers.
  • Use comments to document function purpose, parameters and return values.
  • Prefer clear variable names over excessive commenting.

Common Mistakes

  • Nesting /* */ comments (not allowed in C).
  • Commenting out large blocks with // on only the first line — use /* */ for blocks.
  • Using comments to disable code long-term — use version control instead.

Continue learning with these related tutorials and programs:

Frequently Asked Questions

Are comments executed by the compiler?
No. Comments are ignored during compilation — they exist only for human readers.
Can I nest multi-line comments in C?
No. /* outer /* inner */ outer */ is invalid. Use single-line // comments for nested-style notes.

Related Tutorials

Search tutorials