Comments
Comments are text in your source file that the compiler completely ignores — they exist purely for the humans reading the code, and that includes your future self. C supports two comment styles.
Multi-line comments: /* */
Anything between /* and */ is a comment, no matter how many lines it spans:
/*
* This function computes the average of an array of doubles.
* Returns 0.0 if the array is empty.
*/
double average(double *values, int count) {
if (count == 0) return 0.0;
/* accumulate first, then divide once at the end */
double sum = 0.0;
for (int i = 0; i < count; i++) {
sum += values[i];
}
return sum / count;
}/* outer comment /* inner comment */ this line is NOT a comment anymore, and will likely cause an error */
The safest way to temporarily disable a block of code that already contains /* */ comments is #if 0 ... #endif, which the preprocessor handles differently and doesn't have this nesting problem.
Single-line comments: //
Everything from // to the end of the line is a comment:
int total = 0; // running total across all iterations total += 5; // add the current item's contribution
When to write comments
Good comments explain why, not what. The code itself already shows what it does; a comment restating that adds noise without adding information. Comments earn their keep when they explain a non-obvious reason, a tricky edge case, or an assumption that isn't visible just from reading the code.
Bad — restates the obvious
i++; // increment i by 1
Good — explains a non-obvious reason
i++; // skip the header row before reading data