Nested comment pitfalls
What you'll learn: Why you cannot put one multi-line comment inside another and how to avoid the "broken code" trap it creates.
The logic of the sandwich
In C, a multi-line comment starts with /* and ends with */. You might think of these as a pair of bread slices. Everything between them is the filling that the compiler ignores.
The problem arises when you try to "nest" these comments—putting one block comment inside another. To the compiler, a comment isn't a container; it’s a toggle. The compiler starts ignoring code the moment it sees /*, and it stops the very first time it sees */. It does not keep track of how many "starts" it has seen; it only looks for the very first "end."
When the code breaks
Imagine you have a block of code you’ve already commented out to save for later. Then, you decide to comment out a larger section of the file that happens to include that block.
/*
We are starting a big comment here.
/* This is an old comment about the math below */
int result = 10 + 5;
Wait! The compiler thinks the comment ended right above this line.
*/
In the example above, the compiler sees the */ at the end of the "old comment" and assumes the entire big comment is finished. It then tries to read "Wait! The compiler..." as actual C code. Since that isn't valid C, your program will crash during compilation with a confusing error message.
The "Sticky Note" analogy
Think of a multi-line comment like a giant piece of translucent tape you put over your code. If you put a small piece of tape on a page, and then try to cover the whole page with a larger piece of tape, you might think the whole page is covered.
However, the C compiler is like a machine that peels off the tape the moment it reaches an edge. It sees the edge of your small piece of tape and thinks, "Okay, the covering is gone!" and starts reading the rest of the page, even though you intended for the larger piece to keep everything hidden.
To avoid this, use single-line comments (//) for internal notes, as they don't interfere with the /* ... */ blocks, or use your code editor's built-in "Toggle Comment" feature to handle large blocks safely.
Key takeaway
C comments do not nest; the first */ encountered always ends the comment, regardless of how many /* openers came before it.