Lesson 12 +10 XP

C Comments

C Comments

Comments are notes written for people. The compiler skips them completely, like they're invisible.

Single-line comments

A comment that starts with // runs from the slashes to the end of that line:

C
// This is a single-line comment
printf("Hi");  // comments can sit after code too

Multi-line comments

Wrap several lines with /* and */:

C
/* This comment
   spreads across
   multiple lines. */

What to use them for?

  • Explain the "why" behind code that looks weird.
  • Describe what a function or section does.
  • Leave a TODO for later: // TODO: handle empty input.
  • Temporarily comment out a broken line while you test.

Rules of thumb

  • Keep comments true to the code; a wrong comment is worse than none.
  • Don't point out the obvious already written in the code.
  • Use them for tricky slices, not every line.

TL;DR

  • // comments run to the end of the line.
  • /* ... */ spans many lines.
  • The compiler ignores comments entirely.
  • Use comments to explain "why", note TODOs, and temporarily disable code.