C Programming Tutorial - ITDeveloper

ITDeveloper : C Programming

C Programming

Step by Step Tutorials


Share with a Friend

Comments In C

When we create a program in C language, then we comment on a line inside the program to describe or explain it well. Comment helps the programmer to explain the logic used in that line of the program. Commenting makes the code on that line easier to read and understand.

Comment is a statement that is not executed and run by the compiler. The compiler ignores the comment written inside the programs because the comment is only for the programmer.

 

When the program becomes too long or when the programmer re-works on that program after a long time, then the programmer can easily understand the logic used in that line by reading the comments written in that line. So basically this is the real purpose of commenting.

 

Types of Comments in C Language

There are two types of comments in C language -:

  1. Single line comment
  2. Multi-line comment

1. Single line comment

Single line comment is used in single line only. Single line comment is represented as double forward-slash (//).

Syntax -:

The syntax of single line comment is like this:



// Single line comment
 
Let us understand the single line comment by this example, this example is the 
same for both C and C ++ languages.
 


Example -:



// C program example

// Single Line comment

#include

int main (void)
{
printf ("Welcome to ITDeveloper"); // printing information
return 0; // return zero
}

Output -:

Welcome to ITDeveloper

2. Multi-line comment

Multi line comment starts with an asterisk slash (/) and ends with an asterisk slash (/). You can use multi line comments anywhere in your code. A multi-line comment can be one or more than one line in a program.

Syntax -:

The syntax of the multi-line comment is like this:

//* Comment starts

Line 1

Line 2.

…

Comment ends 

* /

Example

Let us understand the multi-line comment by this example:

// C program example
// Multi Line comment
#include <stdio.h>
int main (void)
{

/*
With the main () function, we start writing programs.
Every program in C language has a main () function from where the program starts.
*/

printf ("Welcome to ITDeveloper
"); // Here I Print some Information return 0; }

Output -:
Welcome to ITDeveloper

You can also comment at the end of the program, which will appear in the last line of the code.

Example -:

  • int age = 40; // age of the person (single line comment)
  • int age = 25; / * age of the person * / (Multi-line comment)