Comments (// and /* */)
C has two types of comments: block comments (/* */) and single-line comments (//), which are available from C99 onward. Comments are replaced with a single space character during preprocessing and have no effect on program execution.
Syntax
/* Block comment — everything between /* and */ is a comment. Use this when you want to write a comment that spans multiple lines. */ int x = 10; /* Can also be used as an inline comment */ // Single-line comment — everything from // to the end of the line is a comment (C99 and later). int y = 20; // Assign 20 to y /* * Documenting functions and files with /* */ is the conventional style. * Aligning asterisks at the start of each line improves readability. */
Comment Types
| Type | Syntax | Description |
|---|---|---|
| Block comment | /* text */ | Everything between /* and */ becomes a comment. Available since C89/C90. Used for multi-line explanations or commenting out a block of code. |
| Single-line comment | // text | Everything from // to the end of the line becomes a comment. Included in the C99 standard. Commonly used to add a short note to the right of a line of code. |
When to Write Comments and When Not To
| Decision | Situation | Reason |
|---|---|---|
| Write a comment | The reason why something was implemented a certain way | Design decisions and trade-offs that cannot be understood from the code alone help your future self and other developers. |
| Write a comment | Complex algorithms or formulas | Add a brief description of what a piece of logic does when its behavior is not immediately obvious at a glance. |
| Write a comment | Public APIs and reusable functions | Documenting parameters, return values, and side effects helps callers understand how to use the function. |
| Write a comment | Temporarily disabled code during debugging | Leaving a note explaining why code was commented out and when it can be removed reduces the risk of forgetting to clean it up later. |
| No comment needed | Logic that is obvious from reading the code | Comments like /* add 1 to i */ become noise. Clear variable and function names remove the need for such comments. |
| No comment needed | Change history or deleted code | Because version control (git) is available, there is no need to keep old code or change dates in comments. |
Sample Code
comment_basic.c
/*
* comment_basic.c — Basic usage of C comment syntax.
* Compile: gcc -std=c99 -o comment_basic comment_basic.c
*/
#include <stdio.h>
/* Define the number of items as a constant */
#define ITEM_COUNT 3
int main(void) {
/* Manage item names and prices in parallel arrays */
const char *items[ITEM_COUNT] = {"widget", "gadget", "device"};
int prices[ITEM_COUNT] = {1200, 3500, 800};
int total = 0; // Running total (before tax)
// Sum up the price of each item
for (int i = 0; i < ITEM_COUNT; i++) {
printf("%-10s %5d yen\n", items[i], prices[i]);
total += prices[i];
}
printf("----------\n");
printf("Total %5d yen\n", total);
return 0;
}
The same logic can also be written as:
$ gcc -std=c99 -o comment_basic comment_basic.c && ./comment_basic widget 1200 yen gadget 3500 yen device 800 yen ---------- Total 5500 yen
comment_function.c
/*
* comment_function.c — The conventional style for documenting functions.
* Compile: gcc -std=c99 -o comment_function comment_function.c
*/
#include <stdio.h>
/*
* calculate_average — Computes the average of an integer array and returns it.
*
* Parameters:
* values — pointer to the integer array
* count — number of elements (must be 1 or greater)
*
* Returns:
* The average as a double. Returns 0.0 if count is 0.
*/
double calculate_average(const int *values, int count) {
if (count <= 0) {
return 0.0; /* Early return to prevent division by zero */
}
int sum = 0;
for (int i = 0; i < count; i++) {
sum += values[i];
}
/* Cast to avoid integer division */
return (double)sum / count;
}
int main(void) {
int scores[] = {85, 92, 78, 95, 60};
int n = 5; // Number of elements
double avg = calculate_average(scores, n);
printf("Average score: %.1f\n", avg);
return 0;
}
The same logic can also be written as:
$ gcc -std=c99 -o comment_function comment_function.c && ./comment_function Average score: 82.0
comment_debug.c
/*
* comment_debug.c — Commenting out code with block comments during debugging.
* Compile: gcc -std=c99 -o comment_debug comment_debug.c
*/
#include <stdio.h>
int main(void) {
int value = 42;
/* Debug: check intermediate value (remove after verification)
printf("DEBUG: value = %d\n", value);
*/
// TODO: add validation logic later
if (value > 0) {
printf("value is positive: %d\n", value);
}
return 0;
}
The same logic can also be written as:
$ gcc -std=c99 -o comment_debug comment_debug.c && ./comment_debug value is positive: 42
Common Mistakes
Nesting block comments
Block comments in C cannot be nested. Placing /* inside an existing /* */ comment does not open a second level — the first */ closes the comment, and everything after it is parsed as code. To comment out a block that already contains /* */ comments, use conditional compilation with #if 0.
/* outer /* inner */ this is parsed as code — NG */ #if 0 /* a line that already has a comment */ int x = 10; /* existing inline comment */ int y = 20; #endif /* #if 0 ... #endif serves as the "comment out" replacement */
Using // comments in C89 environments
Single-line comments (//) are part of the C99 standard and later. They are not available in C89/C90 compilers and will cause a compile error. In embedded or legacy environments that require C89 compliance, use only /* */. Specifying -std=c99 or -std=c11 at compile time makes the intent explicit.
Overview
C has two comment types: /* */ and //. Block comments (/* */) have been available since C89 and are well suited for multi-line explanations and function headers. Single-line comments (//) were standardized in C99 and are commonly used for short notes to the right of a line of code. One important constraint is that block comments cannot be nested — use #if 0 ... #endif to comment out code that already contains block comments. Comments are most effective when they explain "why" rather than "what." Commenting on logic that is already obvious from the code itself becomes noise.
If you find any errors or copyright issues, please contact us.