Skip to content
StudyFame

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 commentMulti-line comment
Symbol///* to start, */ to end
Ends atThe end of the lineThe */ symbol
LinesOne lineOne or many lines
Added inC99 (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

  1. Lines 1–3: a multi-line comment with the program details. Nothing here runs.
  2. int a = 25; // first number: the code runs, the part after // is ignored.
  3. 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

MistakeWhat happensFix
Forgetting */The rest of the file becomes a comment; errors like "unterminated comment"Always close /* with */
Nesting /* */ commentsThe comment ends early; errorsUse // inside, or don't nest
Writing / / with a spaceNot a comment; syntax errorWrite // with no space
Comment says the obvious: i = i + 1; // add 1 to iAdds nothingExplain why, not what

Comments in C Practice Questions

  1. What is a comment? Why is it used?
  2. Name the two types of comments in C.
  3. Which comment style was added in C99?
  4. Are nested comments allowed in C? Why?
  5. Do comments increase the size of the final program?
Show answers
  1. A non-executable note in the code, ignored by the compiler. It is used to explain the code.
  2. Single-line (//) and multi-line (/* */).
  3. Single-line comments (//).
  4. No. A /* */ comment ends at the first */, so the inner one closes the outer one.
  5. 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)

  1. Which symbol starts a single-line comment?

    A) #   B) //   C) /*   D) --

  2. A multi-line comment ends with:

    A) //   B) /*   C) */   D) ;

  3. Who removes comments from the code?

    A) Linker   B) Loader   C) Preprocessor   D) Operating system

  4. Are nested /* */ comments allowed in C?

    A) Yes   B) No   C) Only in C99   D) Only inside main()

  5. What does printf("// hello"); print?

    A) Nothing   B) hello   C) // hello   D) An error

Show answers
  1. B: //
  2. C: */
  3. C: the preprocessor.
  4. B: no.
  5. 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

CommentNon-executable note, ignored by the compiler
TypesSingle-line // and multi-line /* */
// added inC99
Removed byPreprocessor
Nested commentsNot allowed
Effect on speed / sizeNone

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.