Lesson 10 +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:

// This is a single-line comment
cout << "Hi";  // comments can sit after code too

Multi-line comments

Wrap several lines with / and /:

/* 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.
  • Don't bloat: a simple, clear line needs no comment; a tricky slice is where a comment earns its keep.

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.