C++ comments: //, /* */, and how to comment like a pro

Short answer: // comments out the rest of the line; /* ... */ comments out anything between the markers, even across lines. The compiler throws both away before it even starts — proof below.

#include <iostream>
using namespace std;

// This line comment explains the program. The compiler ignores it.

/* Block comments can span
   as many lines as you need. */

int main() {
    cout << "comments are ignored" << endl;  // trailing comment
    // cout << "this line does not run";
    return 0;
}

The output proves the point: commented lines produce no code, no output, no cost. That makes comments a communication tool for humans — and like all communication, most of it is noise.

The two forms

FormExtendsTypical use
// line commentto end of lineExplaining one line or one decision — the everyday default
/* block */until the closing */Temporarily disabling a chunk of code, file-header notes

The trap: block comments don't nest

/*
  Disabling this debug block...
  /* inner comment */    ← the block ENDS here!
  cout << "this is NOT commented out";   // ← surprise, it's live code
*/                        // ← stray */ — syntax error

The first */ closes the block, no matter how many /* opened. This is why debugging-by-block-comment goes wrong in confusing ways. For temporarily disabling code, prefer #if 0 … #endif (which does nest) or just delete it — version control remembers.

What separates good comments from noise

Comments in the wild (interview angle)

  1. "Do comments affect performance?" — no; they're stripped before compilation. Zero runtime cost, tiny compile-time cost.
  2. "Can block comments nest?" — no; the first */ wins (see trap above). A surprisingly common screening question.
  3. "What are doc comments?" — ///-style annotations that documentation generators turn into reference docs; standard practice for public APIs.
  4. "How would you review a 300-line diff with no comments?" — the real question behind the question: does the code explain its why? Names can carry a lot; comments carry what names can't.

Practice

Related: the Hello World lesson (where comments first appear), what int main() is, and the while loop explained.