Comments in C Programming
A comment is a note you write inside your code for humans. The compiler completely ignores it.
Comments make your program easy to understand, for your teacher, your teammates, and for you when you open the code after a month. In this lesson you will learn the two types of comments in C and the rules to use them correctly.
What You Will Learn
- What a comment is and why we use it.
- Single-line comments
//and multi-line comments/* */. - Why nested comments are not allowed.
- How to use comments to test and debug code.
- How to write good comments.
Comments in C Explained
Exam definition: A comment is a non-executable statement used to explain the code. It is ignored by the compiler. C has two types: single-line comments (//) and multi-line comments (/* ... */).
| Single-line comment | Multi-line comment | |
|---|---|---|
| Symbol | // | /* to start, */ to end |
| Ends at | The end of the line | The */ symbol |
| Lines | One line | One or many lines |
| Added in | C99 (borrowed from C++) | Original C (1972) |
Memory trick: // = two slashes, one line. /* */ = a sandwich: everything between the two slices is a comment.
Why use comments?
- To explain what a piece of code does.
- To write details like program name, author and date (documentation section).
- To stop a line from running while testing, without deleting it.
Syntax of Comments in C
// This is a single-line comment
/* This is a
multi-line comment */
int marks = 90; // a comment can also come after code
Example 1: C Program with Both Types of Comments
/* Program: Add two numbers
Author : Riya Sharma
Date : 15-11-2026 */
#include <stdio.h>
int main(void)
{
int a = 25; // first number
int b = 15; // second number
int sum = a + b; // add them
printf("Sum = %d\n", sum);
return 0;
}
Output
Sum = 40
Code Explanation
- Lines 1–3: a multi-line comment with the program details. Nothing here runs.
int a = 25; // first number: the code runs, the part after//is ignored.- The output only shows
Sum = 40. Comments never appear in the output.
How It Works
- Before compilation, the preprocessor removes every comment and puts a space in its place.
- So comments do not make your program bigger or slower.
- Inside a string,
//and/*are just normal text:printf("// not a comment");prints// not a comment.
Nested comments are not allowed
A multi-line comment ends at the first */. So you cannot put one /* */ comment inside another:
/* outer comment
/* inner comment */
this line is now treated as code!
*/
The first */ closes the comment, so the next lines become code and the compiler shows errors.
Example 2: Using Comments to Debug (Comment Out a Line)
To test a program without one line, put // in front of it instead of deleting it:
#include <stdio.h>
int main(void)
{
printf("Step 1\n");
// printf("Step 2\n");
printf("Step 3\n");
return 0;
}
Output
Step 1 Step 3
"Step 2" is not printed because that line is now a comment. Remove the // to bring it back.
Important Points to Remember
- Comments are ignored by the compiler (removed by the preprocessor).
//= single line./* */= multiple lines.//comments were added in C99.- Nested
/* */comments are not allowed. - Comments do not slow down the program.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
Forgetting */ | The rest of the file becomes a comment; errors like "unterminated comment" | Always close /* with */ |
Nesting /* */ comments | The comment ends early; errors | Use // inside, or don't nest |
Writing / / with a space | Not a comment; syntax error | Write // with no space |
Comment says the obvious: i = i + 1; // add 1 to i | Adds nothing | Explain why, not what |
Comments in C Practice Questions
- What is a comment? Why is it used?
- Name the two types of comments in C.
- Which comment style was added in C99?
- Are nested comments allowed in C? Why?
- Do comments increase the size of the final program?
Show answers
- A non-executable note in the code, ignored by the compiler. It is used to explain the code.
- Single-line (
//) and multi-line (/* */). - Single-line comments (
//). - No. A
/* */comment ends at the first*/, so the inner one closes the outer one. - No. The preprocessor removes them before compilation.
Coding Exercise
Write a program that prints the area of a square with side 6. Add a multi-line comment at the top with your name and the program name, and a single-line comment on every line inside main().
Show solution
/* Program: Area of a square
Author : Riya Sharma */
#include <stdio.h>
int main(void)
{
int side = 6; // length of one side
int area = side * side; // area = side x side
printf("Area = %d\n", area);// show the result
return 0; // program ended successfully
}
Area = 36
Quick Quiz: Comments in C (MCQ)
-
Which symbol starts a single-line comment?
A)
#B)//C)/*D)-- -
A multi-line comment ends with:
A)
//B)/*C)*/D); -
Who removes comments from the code?
A) Linker B) Loader C) Preprocessor D) Operating system
-
Are nested
/* */comments allowed in C?A) Yes B) No C) Only in C99 D) Only inside
main() -
What does
printf("// hello");print?A) Nothing B)
helloC)// helloD) An error
Show answers
- B:
// - C:
*/ - C: the preprocessor.
- B: no.
- C: inside a string,
//is normal text.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Comment | Non-executable note, ignored by the compiler |
|---|---|
| Types | Single-line // and multi-line /* */ |
// added in | C99 |
| Removed by | Preprocessor |
| Nested comments | Not allowed |
| Effect on speed / size | None |
Frequently asked questions
- Why are nested comments not allowed in C?
- A comment ends at the first
*/, so the inner comment closes the outer one. - Do comments affect the speed of a program?
- No. They are removed before compilation.
- Is // valid in all C compilers?
- It is valid from C99 onwards; very old C89 compilers do not accept it.
- What is "commenting out" code?
- Turning a line into a comment to stop it from running without deleting it.
Remember
//= one line./* */= sandwich (many lines).- Good comments explain why, not what.
Next Topic
Next lesson: Keywords and Identifiers in C. You will learn C's reserved words and the rules for naming your variables.