When you read a textbook, you often write small notes in the margin like "important" or "this formula is used for area". These notes help you, but they are not part of the actual text.
C programs can have similar notes too. They are called comments.
What is a Comment?
A comment is a piece of text in a program that is ignored by the compiler. It is written only for humans who read the code.
Comments are used to:
Explain what a part of the program does and why it is written that way
Add details such as the program's purpose or author
Temporarily disable a line of code while testing
Because the compiler ignores them, comments do not change the output of a program and do not increase the size of the final executable file.
Types of Comments in C
C supports two kinds of comments:
Type | Syntax | Used For |
|---|---|---|
Single-line comment |
| Short notes on one line |
Multi-line comment |
| Longer notes that may span many lines |
1. Single-Line Comment //
A single-line comment starts with //. Everything after // until the end of that line is ignored.
// This program prints a greeting
printf("Hello"); // this comment is on the same line as code
The code before // still runs. Only the part after // is treated as a comment.
The // style was officially added to C in the C99 standard. A compiler running in strict C89/C90 mode may reject it, but every modern compiler accepts it by default.
2. Multi-Line Comment /* */
A multi-line comment starts with /* and ends with */. Everything in between is ignored, even if it covers several lines.
/*
This program calculates
the area of a rectangle.
*/
It can also be used on a single line, or even in the middle of a line:
int marks = 90; /* out of 100 */
This style is also called a block comment, and it has been part of C from the very beginning.
Complete Example
/*
* Program: Area of a rectangle
* Author : Asha
* Purpose: Shows how comments are written in C
*/
#include <stdio.h>
int main()
{
int length = 10; // length in cm
int width = 5; // width in cm
/* area = length x width */
int area = length * width;
printf("Area = %d\n", area); // prints the result
// printf("This line is commented out\n");
return 0;
}
(Don't worry about int length = 10; for now. These lines create variables, which we will learn in the very next topic.)
Output:
Area = 50
Notice that the last printf() did not run. Putting // in front of it turned the whole line into a comment. This is called commenting out code, and it is a quick way to test a program without deleting lines.
How the Compiler Treats Comments
Before the real compilation starts, every comment is removed and replaced by a single space. So the part of the compiler that translates your code never even sees your comments.
Your code What the compiler works with
--------- ----------------------------
int area = l * w; // area → int area = l * w;
int/* note */x = 5; → int x = 5;
Because a comment becomes a space, int/* note */x = 5; is the same as int x = 5;. A comment can never join two words together.
Comments Inside Strings Are Not Comments
If // or /* appears inside double quotes, it is just normal text in the string.
#include <stdio.h>
int main()
{
printf("/* This is not a comment */\n");
printf("// Neither is this\n");
return 0;
}
Output:
/* This is not a comment */
// Neither is this
Common Mistakes
1. Nesting Multi-Line Comments
Multi-line comments cannot be nested in C. A comment ends at the first */ it finds.
/* outer comment /* inner comment */ still inside? */
Here the comment ends right after inner comment. The words still inside? */ are treated as code, so the compiler reports an error.
/* outer comment /* inner comment */ still inside? */
└──────────── comment ──────────────┘ └─ treated as code → error
The second /* has no special meaning because it is already inside a comment.
2. Forgetting to Close /*
If you write /* but forget */, everything after it becomes part of the comment, possibly the rest of your program. The compiler then reports an error such as "unterminated comment".
3. A Backslash at the End of a // Comment
This one is surprising. If a single-line comment ends with a backslash \, the next line is joined to it and also becomes part of the comment.
#include <stdio.h>
int main()
{
int total = 10;
// Add the bonus marks \
total = total + 5;
printf("Total = %d\n", total);
return 0;
}
Output:
Total = 10
The line total = total + 5; never runs, because the backslash joined it to the comment above. Lines ending in \ are joined before comments are removed. Many compilers warn about this ("multi-line // comment"), so don't ignore warnings.
4. Writing Comments That Repeat the Code
i = i + 1; // add 1 to i
This comment adds nothing. Anyone can see that 1 is being added. A better comment explains the reason:
i = i + 1; // skip the header row
Writing Good Comments
Explain why, not what. The code already shows what happens.
Keep comments up to date. A wrong comment is worse than no comment.
Use a header comment at the top of a file or above a function to describe its purpose.
Use meaningful variable names so that fewer comments are needed.
int totalMarks;explains itself better thanint t; // total marks.Don't leave large blocks of old commented-out code in finished programs. It makes the file harder to read.
// vs /* */
Point |
|
|
|---|---|---|
Ends at | End of the line | The first |
Can span many lines | No (one line each) | Yes |
Can be placed in the middle of a line of code | No, it hides the rest of the line | Yes |
Available since | C99 | The original C |
Can be nested | Not applicable | No |
Interview Questions
Do comments increase the size of the executable?
No. Comments are removed before compilation, so they never become part of the machine code.
Can multi-line comments be nested in C?
No. A /* comment ends at the first */. Any /* inside a comment is ignored.
What does the compiler replace a comment with?
Each comment is replaced by one space character.
Is // text inside a string literal a comment?
No. Inside double quotes, // and /* are ordinary characters of the string.
Which C standard introduced // comments?
C99. Before that, only /* */ comments were part of standard C.
In Simple Words
A comment is a note for humans. The compiler ignores it completely.
Remember the two forms:
// text → comment until the end of the line
/* text */ → comment until the first */
Use comments to explain the purpose of your code, keep them short and correct, and remember that /* */ comments cannot be nested.
Now that we can write and explain simple programs, let's learn how to store values in them using variables.