Understand the Comments in C

Comments in C

Let’s suppose that you want to add description in your program but you don’t want it to generate error or stop your program compilation or code that you don’t want compiler to compile. C provides you the feature called “comments” to deal with such situations. You can comment single line or multiple lines at a time.

You can write short description in your program like – What program does?, What the particular function does?, Description for the variable, etc.

During the execution of the program C compiler will skip each word which is in commented area.

In C language there are 2 types of comments

  1. Single line comment (“//”)
  2. Multi-line comment (“/*    */”)

Single line comment:

As the name says this type of comment will be applied for only one line.

You can comment single line with adding “//”  at beginning of the line as sown in below example.

Good for describing variables, functions, etc.

Example: 

//printf(“itvoyagers.in”)

                                         or

void display()                            // This function is use to display some text——- We can use comments do describe any functions or variables

{

    printf(“Hello Voyagers!!!”);                  

}

Multi-line comment:

This type of comments will be applied on two ore more lines.

We can use it to comment out any block of code or function.

You can comments multiple at same time with the adding “/*” at the beginning of the paragraph and by adding “*/” at the end of the paragraph as shown in below example.

Good for describing the program.

Example:

/*

void display()                            // This function is use to display some text—- Multi-line comment is use to hide the block of code

{

    printf(“Hello Voyagers!!!”)                       

}

*/

                                         or

/*

Following program is accepts 2 numbers and displays their addition.

Multi-line comment can be use to describe complete program so that others can understand the program

*/

void main()

{

    int a, b;

    printf(“Enter any 2 numbers”);

    scanf(“%d %d”, &a, &b);

    printf(“Sum of both the number is : %d”, a+b);

}

Leave a Comment