TExcelTAD
Home › C › C Comments

C Comments

Comments are notes for humans. The compiler throws them away before it translates your program, so they never change what the program does. Good comments explain why code exists or what a tricky part is doing; they make your code easier to read for others — and for yourself a few months later.

C has two kinds of comments.

Single-line comments

A single-line comment starts with // and continues to the end of the line. It can sit on a line of its own or after some code.

c · try it yourself
#include <stdio.h>

int main(void) {
    // Greet the user
    printf("Hello!\n");   // this comment follows a statement
    // printf("This line is disabled\n");
    return 0;
}
Hello!

The third printf never runs, because the whole line is a comment. "Commenting out" code like this is a quick way to switch something off while testing.

Note: // comments were added in the C99 standard. Very old C89 compilers only understand /* ... */, but every modern compiler accepts both.

Multi-line comments

A multi-line (block) comment starts with /* and ends with */. Everything between them is ignored, even across many lines.

c · try it yourself
#include <stdio.h>

/*
 * Program: area.c
 * Prints the area of a 4 x 6 rectangle.
 * The leading stars on each line are just a common style.
 */
int main(void) {
    printf("Area: %d\n", 4 * 6);   /* width * height */
    return 0;
}
Area: 24

Block comments can also appear in the middle of a line:

c · try it yourself
#include <stdio.h>

int main(void) {
    printf("Total: %d\n", 10 /* apples */ + 5 /* pears */);
    return 0;
}
Total: 15

Block comments do not nest

A block comment ends at the first */ the compiler finds. Putting one block comment inside another therefore breaks:

c
/* outer comment starts
   /* inner comment */
   this text is no longer inside a comment!  -> compile error
*/

If you need to disable a large piece of code that already contains block comments, use the preprocessor instead:

c · try it yourself
#include <stdio.h>

int main(void) {
#if 0
    /* everything here is skipped,
       including this comment */
    printf("never compiled\n");
#endif
    printf("Only this line runs.\n");
    return 0;
}
Only this line runs.

#if 0 ... #endif removes the code before compilation, and it nests safely.

Which one should I use?

Style Best for
// ... Short notes on a single line, end-of-line remarks, temporarily disabling one line
/* ... */ File headers, function descriptions, comments in the middle of an expression
#if 0 ... #endif Disabling a large block of code

Writing useful comments

  • Explain intent, not the obvious: // retry because the sensor sometimes returns 0 is helpful; // add 1 to i above i++ is noise.
  • Keep comments up to date. A wrong comment is worse than none.
  • Describe what a function expects and returns right above its definition.

Tip: Clear variable and function names reduce the number of comments you need. int seconds_per_day = 86400; explains itself.

Test yourself

Which syntax starts a single-line comment in C?

  1. #
  2. //
  3. --
  4. <!--

What happens if you put /* a /* b */ c */ in your code?

  1. Everything is a comment
  2. The comment ends at the first */, so c */ is treated as code
  3. The compiler nests the comments
  4. Only b is a comment

Do comments make the compiled program larger or slower?

  1. Yes, slightly
  2. Yes, a lot
  3. No, they are removed before compilation
  4. Only block comments do