Nearby lessons

7 of 72

C - Comments

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

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