C ยท Chapter 6 of 45

C Comments

Comments are notes in your code that the compiler ignores. They are used to explain what code does, making it easier for you and others to understand later.

C supports single-line comments starting with //, and multi-line comments enclosed between /* and */.

Syntax
// single-line comment
/* multi-line
   comment */

Single-line comments

Anything after // on a line is ignored by the compiler. These are great for short explanations next to a line of code.

Multi-line comments

Text between /* and */ can span multiple lines and is often used for longer explanations or temporarily disabling blocks of code.

Example 1 (c)
#include <stdio.h>

int main() {
  // This prints a greeting
  printf("Hello!\n");
  return 0;
}
Output
Hello!

The single-line comment is ignored during compilation.

Example 2 (c)
#include <stdio.h>

/* This program
   prints a number */
int main() {
  printf("%d\n", 42);
  return 0;
}
Output
42

The multi-line comment describes the program above main().

Key points

  • // starts a single-line comment.
  • /* ... */ wraps a multi-line comment.
  • Comments are ignored by the compiler.
  • Good comments explain why, not just what, the code does.
๐Ÿ’ก Note: Overusing comments to state the obvious can clutter code; write comments that add real value.

๐Ÿ“ Quick Quiz

1. Which symbol starts a single-line comment in C?

2. How do you write a multi-line comment?

3. Are comments compiled into the executable?