Comments in C

A comment in C is a text that is ignored by the compiler. Let us see how we can use comments in a C program.

There are two types of comments in C:

  • Single line comments
  • Multi line comments

Single line comments

A single line comment can be made by putting // (double slash) before a text in a single line.

#include<stdio.h>
int main()
{
    //The line below will print Hello World
    printf("Hello World");
    return 0;
}

Multi line comments

A multi line comment can be made by putting the comment text between /* and */

#include<stdio.h>
int main()
{
    /*The line below
    will print Hello World*/
    printf("Hello World");
    return 0;
}

You can place a comment anywhere in your code, before a line or after a line.

printf("Hello World");//This line will print Hello World

It is totally up to us which format we use for a comment. In general, a single line comment is used for short comments whereas a multiline comment is used for long comments.

Uses of comments

  • A comment is used to describe what has done in the code. It could be useful if you want some other person to understand your code. In some cases, it could be useful for you as well. If you are going to look at your code after a long time, a comment will help you to remember what you have done.
  • As comments are ignored by the compiler, they are also used to temporary disable some parts of the code. This could be required when you are debugging the code. Commenting a section of code and then testing it helps in debugging.