Nearby lessons

14 of 124

C - Comments in C

Comments are notes you leave inside your source code for people to read — the compiler skips them completely. C gives you two styles: // for a single line and /* ... */ for a block. This lesson covers both, what comments are good for, and the one rule beginners always trip over: block comments cannot be nested.

What are Comments?

A comment is a note you write inside your source code to explain what the code does. Comments are written for people (including your future self) — the compiler skips them completely while building the program.

In simple words: a comment is a sticky note on your code. It costs nothing to run, but it saves hours when you (or someone else) reads the code later.
Example01
CCode Cell
1#include <stdio.h>
2 
3int main() {
4 // This line prints a message
5 printf("Hello\n"); // comments can follow code too
6 return 0;
7}
Output
Hello

Single-Line Comments (//)

Everything from // to the end of that line is a comment. Use it for one-line notes.

In simple words: // comments out the rest of the line — and only that line. The next line is code again.
Example02
CCode Cell
1#include <stdio.h>
2 
3int main() {
4 int age = 20; // this is a comment
5 // the whole of this line is a comment too
6 printf("%d\n", age);
7 return 0;
8}
Output
20

Multi-Line Comments (/* */)

To write a note that spans several lines, open with /* and close with */. Everything in between is ignored by the compiler. This is the usual way to write a header block at the top of a file.

Example03
CCode Cell
1#include <stdio.h>
2 
3/*
4 Author : CodingsPoints
5 Program : Demo of multi-line comments
6 Purpose : Show how block comments work
7*/
8 
9int main() {
10 printf("Multi-line comment demo\n");
11 return 0;
12}
Output
Multi-line comment demo

Block Comments Do Not Nest

This is the one comment rule that catches everybody. A block comment ends at the first */ the compiler finds — not at the matching one. So you cannot put a /* */ comment inside another /* */ comment.

Common Mistake: writing /* or */ inside a block comment.
/*
   Purpose  : Show how /* */ works
*/

The comment closes at the */ in the middle of the sentence. The word works and the final */ are then left over as real code, and the compiler stops:

error: unknown type name 'works'
error: expected identifier or '(' before '/' token

The fix is simply to describe the syntax without typing it:

Instead of Write
Purpose : Show how /* */ works Purpose : Show how block comments work
Commenting out a block that already has /* */ in it Put // in front of each line instead
In simple words: one /* is closed by the very next */. If you need to comment out code that already contains a block comment, use // on every line — single-line comments stack safely.
Example04
CCode Cell
1#include <stdio.h>
2 
3int main() {
4 // Safe way to disable several lines - // stacks without any nesting problem
5 // int a = 10;
6 // int b = 20;
7 // printf("%d\n", a + b);
8 
9 printf("Nothing above me ran\n");
10 return 0;
11}
Output
Nothing above me ran

What Comments Can Do

  • Explain tricky logic — a one-line note can save hours of reading.
  • Document programs — header comments for author, date, purpose.
  • Temporarily disable code — commenting a line stops it from running without deleting it.
Quick Check: does // printf("hi"); print anything? No — the whole line is a comment, so the compiler never sees the printf at all.
📝 Key Takeaways
  • Comments explain code but are ignored by the compiler
  • // marks a single-line comment
  • /* ... */ marks a multi-line comment
  • Comments never affect the output of a program
  • Block comments do not nest — the first */ closes the comment

🧠 Test Your Knowledge

5 Questions
Progress: 0 / 5