Lesson 8 of 60 – Comments in C
13%

Comments in C

Comments are notes written inside a C program to explain the code. They are mainly used to make programs easier to understand and maintain. The compiler does not treat comments as executable program statements.

Note: C supports both single-line comments using // and multi-line comments using /* */.

1. What are Comments?

Comments are explanatory notes added to source code. They help programmers understand what a particular part of the program does.

// This program displays a message

2. Why Use Comments?

Comments are useful because they can:

  • Explain difficult code.
  • Describe the purpose of a program.
  • Make code easier to maintain.
  • Help beginners understand programs.
  • Document important logic.

3. Types of Comments in C

C commonly uses two types of comments:

  1. Single-line comments
  2. Multi-line comments

4. Single-Line Comments

A single-line comment begins with two forward slashes: //

// This is a single-line comment

The comment continues until the end of that line.

5. Simple Single-Line Example

#include <stdio.h>

int main()
{
    // Display Hello World
    printf("Hello World");

    return 0;
}

6. Comment After a Statement

A single-line comment can also be written after a statement.

int age = 20; // Store age

The part after // is treated as a comment.

7. Multi-Line Comments

A multi-line comment starts with /* and ends with */.

/*
   This is a
   multi-line comment
*/

8. Multi-Line Comment Example

#include <stdio.h>

int main()
{
    /*
       Display a welcome message
       to the user
    */

    printf("Welcome to C");

    return 0;
}

9. Comments are Ignored by the Compiler

Comments are not treated as executable instructions. They are removed or otherwise ignored during the preprocessing stages before the compiler processes the resulting source.

// This does not print anything

10. Commenting a Variable

A comment can explain the purpose of a variable.

int marks = 85; // Student marks

This makes the purpose of marks clear to another programmer.

11. Commenting Multiple Variables

int age = 20;       // Student age
float fee = 5000.0; // Course fee
char grade = 'A';   // Student grade

Each comment explains the corresponding variable.

12. Comments Before Code

Comments can be placed before a block of code to explain its purpose.

// Calculate the total
int total = price + tax;

13. Comments Inside main()

#include <stdio.h>

int main()
{
    // Start of program
    printf("Hello");

    // End the program
    return 0;
}

14. Documenting a Function

Comments can explain what a function is designed to do.

// Displays a welcome message
void welcome()
{
    printf("Welcome");
}

15. Commenting Function Parameters

// Adds two numbers
int add(int a, int b)
{
    return a + b;
}

The comment gives a quick description of the function's purpose.

16. Commenting a Calculation

int length = 10;
int width = 5;

// Calculate rectangle area
int area = length * width;

17. Comments for Conditions

Comments can explain the purpose of a condition.

if (age >= 18)
{
    // Person is eligible
    printf("Eligible");
}

18. Comments for Loops

// Print numbers from 1 to 5
for (int i = 1; i <= 5; i++)
{
    printf("%d\n", i);
}

19. Temporarily Disable Code

Comments can be useful when temporarily disabling a line of code during testing.

printf("Hello");

// printf("This line is temporarily disabled");

The commented statement will not execute.

20. Commenting Several Lines

Several lines can be commented using a multi-line comment.

/*
printf("Line 1");
printf("Line 2");
printf("Line 3");
*/

21. Difference Between // and /* */

Comment Type Syntax Use
Single-line // Usually for one line
Multi-line /* */ Can span multiple lines

22. Comments and Program Output

#include <stdio.h>

int main()
{
    // This comment does not appear in output
    printf("Hello World");

    return 0;
}

Output:

Hello World

The comment itself is not displayed.

23. Good Comments

Good comments should be:

  • Short and clear.
  • Relevant to the code.
  • Easy to understand.
  • Updated when the related code changes.
// Calculate total price

24. Avoid Unnecessary Comments

Comments should add useful information. Avoid comments that simply repeat obvious code.

int age = 20; // Declare age variable

A more useful comment should explain something that is not already obvious, when such an explanation is needed.

25. Header Comment

A program can begin with a comment describing the purpose of the file.

/*
   Program: Student Marks
   Purpose: Calculate total marks
*/

#include <stdio.h>

26. Commenting a Complete Program

/*
   Program to calculate two numbers
*/

#include <stdio.h>

int main()
{
    int a = 10;
    int b = 20;

    // Calculate the sum
    int sum = a + b;

    printf("Sum = %d", sum);

    return 0;
}

27. Nested Comments

C block comments should not be treated as nested block comments. Avoid placing one /* ... */ comment inside another.

/*
   Outer comment

   /* Inner comment */

   Avoid this style
*/

Use separate comments or single-line comments when necessary.

28. Comments Improve Readability

Large programs can contain hundreds or thousands of lines of code. Well-written comments can help developers quickly understand important parts of the program.

// Calculate final bill after applying discount
finalBill = bill - discount;

29. Comment Practice

Add useful comments to the following program:

#include <stdio.h>

int main()
{
    int a = 10;
    int b = 20;
    int sum = a + b;

    printf("Sum = %d", sum);

    return 0;
}

Try explaining the purpose of the variables and calculation using comments.

30. Complete Comments Example

/*
   Program to calculate the sum
   of two numbers
*/

#include <stdio.h>

int main()
{
    // Declare two numbers
    int a = 10;
    int b = 20;

    // Calculate the sum
    int sum = a + b;

    // Display the result
    printf("Sum = %d", sum);

    return 0;
}

This example shows how comments can document different sections of a simple C program.

📌 Key Points

  • Comments are used to explain C source code.
  • Comments are not executable program statements.
  • // is used for single-line comments.
  • /* */ is used for multi-line comments.
  • Comments can explain variables, functions, conditions, and calculations.
  • Comments can temporarily disable code during testing.
  • Good comments should be clear, useful, and relevant.
  • Block comments should not be nested.

🧠 Quick Quiz

Question: Which symbol is used to start a single-line comment in C?