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.
// and multi-line comments using /* */.
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
Comments are useful because they can:
C commonly uses two types of 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.
#include <stdio.h>
int main()
{
// Display Hello World
printf("Hello World");
return 0;
}
A single-line comment can also be written after a statement.
int age = 20; // Store age
The part after // is treated as a comment.
A multi-line comment starts with /* and ends with
*/.
/*
This is a
multi-line comment
*/
#include <stdio.h>
int main()
{
/*
Display a welcome message
to the user
*/
printf("Welcome to C");
return 0;
}
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
A comment can explain the purpose of a variable.
int marks = 85; // Student marks
This makes the purpose of marks clear to another programmer.
int age = 20; // Student age
float fee = 5000.0; // Course fee
char grade = 'A'; // Student grade
Each comment explains the corresponding variable.
Comments can be placed before a block of code to explain its purpose.
// Calculate the total
int total = price + tax;
#include <stdio.h>
int main()
{
// Start of program
printf("Hello");
// End the program
return 0;
}
Comments can explain what a function is designed to do.
// Displays a welcome message
void welcome()
{
printf("Welcome");
}
// Adds two numbers
int add(int a, int b)
{
return a + b;
}
The comment gives a quick description of the function's purpose.
int length = 10;
int width = 5;
// Calculate rectangle area
int area = length * width;
Comments can explain the purpose of a condition.
if (age >= 18)
{
// Person is eligible
printf("Eligible");
}
// Print numbers from 1 to 5
for (int i = 1; i <= 5; i++)
{
printf("%d\n", i);
}
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.
Several lines can be commented using a multi-line comment.
/*
printf("Line 1");
printf("Line 2");
printf("Line 3");
*/
| Comment Type | Syntax | Use |
|---|---|---|
| Single-line | // |
Usually for one line |
| Multi-line | /* */ |
Can span multiple lines |
#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.
Good comments should be:
// Calculate total price
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.
A program can begin with a comment describing the purpose of the file.
/*
Program: Student Marks
Purpose: Calculate total marks
*/
#include <stdio.h>
/*
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;
}
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.
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;
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.
/*
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.
// is used for single-line comments./* */ is used for multi-line comments.Question: Which symbol is used to start a single-line comment in C?