Menu

Comments in C: // and /* */ Explained

C has two comment styles - single-line // and multi-line /* */ - with different histories and one nesting trap. Here is how to use both, plus what is worth commenting and what is not.

This page includes runnable editors - edit, run, and see output instantly.

A comment is text the compiler throws away. It exists purely for the people reading the code later, one of whom is usually you. C offers two forms, and knowing when each is the right tool takes about two minutes.

The Two Forms

Run it: the output is one line. Both comments were deleted before the compiler ever parsed the program - they cost nothing at runtime and add nothing to the executable.

// runs to the end of the physical line. Nothing can follow it on that line, so this does not work the way it looks:

int x = 5;  // set x to five  int y = 6;   /* y is never declared */

/* ... */ ends at the first */, wherever that is. It can start and end mid-line, which is occasionally useful:

int total = price /* before tax */ + shipping;

Why Two Styles Exist

/* */ is original C, from 1972. // came from C++ and was only officially added to C in C99. That history explains something you will notice reading older code: libraries written to be portable to C89 use /* */ even for one-line comments, because // would fail to compile on the old toolchains they still supported.

Today every compiler you are likely to use accepts both. Use // for ordinary remarks and /* */ when a comment genuinely spans lines. If you are targeting a very old embedded compiler, check before relying on //.

Comments Cannot Nest

This is the one real trap:

/* Disable this section for now
   int a = compute();
   /* the classic helper - keep an eye on it */
   int b = a * 2;
*/

The block comment ends at the first */, which is the one on line 3. Lines 4 and 5 are then live code again, and the trailing */ on line 6 is a syntax error. The compiler's message points at the last line and is completely unhelpful about the cause.

The fix is to use the preprocessor instead, which does handle nesting:

#if 0
    int a = compute();
    /* the classic helper - keep an eye on it */
    int b = a * 2;
#endif

#if 0 is never true, so the preprocessor deletes everything up to #endif before the compiler sees it. It survives comments, quotes, and other #if blocks inside, and it is easy to search for when you clean up.

Commenting Out Code While Debugging

Temporarily removing a line is the most common everyday use of comments. When a program misbehaves, disabling one statement at a time tells you which one matters.

Uncomment the printf and run again to watch the loop build up its answer. Tracing with prints is not elegant, but in C it is fast and it always works - a debugger tells you more, and a printf tells you something immediately.

Two habits keep this from becoming a mess. Delete commented-out code before you commit it; version control remembers the old version so you do not have to. And when you leave a disabled line in deliberately, say why in a note beside it.

Documentation Comments

A block comment above a function is where you explain what it does, what its parameters mean, and anything surprising about it.

Tools such as Doxygen read structured comments like these and generate reference documentation. Doxygen's own style uses /** ... */ with @param and @return tags:

/**
 * Converts Celsius to Fahrenheit.
 * @param c temperature in Celsius
 * @return the same temperature in Fahrenheit
 */
double celsius_to_fahrenheit(double c);

Either is fine for your own code. What matters is that the comment lives next to the declaration people read - in the header file, typically - rather than buried in the implementation.

What Is Worth Commenting

The rule that survives contact with real codebases: comment the why, not the what.

i++;  // increment i          <- says nothing the code did not
/* Skip the BOM: files exported by the old system start with
   three bytes that are not part of the data. */
offset += 3;

The second comment contains information that is nowhere in the code. The first is noise that will eventually contradict the line it describes, because comments do not get updated when code changes.

Things genuinely worth a comment in C specifically:

  • Who owns this memory. If a function returns a pointer the caller must free, say so. C has no way to express that in the type.
  • Units and ranges. int timeout; is ambiguous - seconds or milliseconds?
  • Non-obvious correctness. Why the loop stops at n - 1, why this cast is safe, why the buffer is 256 bytes.
  • Deliberate weirdness. Code that looks like a bug but is not attracts "fixes" from future readers unless it is labelled.

That comment earns its place: the line below it looks redundant and is not.

Comments Inside Strings Are Not Comments

One last detail. Comment markers have no special meaning inside a string literal or a character constant:

Both lines print in full. The compiler tokenizes strings before it looks for comments, so // inside quotes is simply two characters. (The %% in the first line is how you print a literal percent sign with printf - % alone starts a format specifier.)

Frequently Asked Questions

How do you write a comment in C?

Two ways. // this is a comment runs to the end of the line. /* this is a comment */ can span any number of lines and ends at the closing */. Both are removed before compilation, so they never affect the program.

Does C support // comments?

Yes, since C99. They were borrowed from C++ and are universally supported today. Only truly ancient C89 compilers reject them, which is why very old code uses /* */ for everything, even one-liners.

Can you nest comments in C?

No. /* outer /* inner */ still outer */ ends at the first */, leaving still outer */ as broken code. To disable a block that already contains /* */ comments, use #if 0 ... #endif instead, which nests correctly.

How do you comment out a block of code in C?

Wrap it in /* */ if it has no block comments inside, or prefix each line with //. The robust option for large regions is #if 0 before and #endif after - the preprocessor removes everything between, and it survives comments and quotes inside.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED