In C, you can add comments to your code to provide explanations or notes for yourself or other programmers. Comments are ignored by the compiler, so they don't affect the behavior of your program. There are two types of comments in C:
- Single-line comments: These start with
//
and continue until the end of the line. For example: - Multi-line comments: These start with
/*
and end with*/
. You can use multi-line comments to add longer explanations or to temporarily disable code during debugging.
For example:
#include <stdio.h>
int main() {
// This is a single-line comment
// Declare two integer variables
int num1 = 10, num2 = 20;
// Calculate the sum of the two numbers
int sum = num1 + num2;
/*
This is a multi-line comment.
You can write as many lines as you want.
*/
/* Output the result to the console
Note: %d is a format specifier that represents an integer value */
printf("The sum of %d and %d is %d.\n", num1, num2, sum);
return 0;
}
In this program, we're using both single-line and multi-line comments to explain what the code does. The comments are not necessary for the program to run, but they make the code more readable and easier to understand.
Comments