Comments:

Comments are used to explain your code and they have nothing to do with the compiler. Compiler ignores your comment and do not consider them the part of your code. They just make your code more readable.

Why Comments :

  • If you have written code for a calculator and if you want to tell someone (who reads the code) that what my code is doing, you do this with the help of comments.
  • If you have written code today and you do not know when will you review your code in future. Maybe you check it after a year. Chances are there that you will forget that which function is doing which work. You will have to thoroughly go through your code. But if you write comment for each function like in case of calculator, first function is doing addition, second one is doing subtraction and so on. In this way you will understand the code at once.
  • If you do not want some piece of you code, you comment it and it will be ignored by the compiler. In future, if you need it, you can uncomment it.


Types :

There are basically two types of comments.

  • Single Lined Coments
  • Multi Lined Comments
The details about single and multilined comments is provided below.

Single Lined Comment:

If you want to comment a single line, you wil have to put two forward slashes before that line.

singlelinedcmt.c
#include <stdio.h>

int main() {
  // Printing Hello World! on screen
  printf("Hello World!");
  return 0;
}

Multi Lined Comment:

If you want to comment multiple lines, put /* at the start of the first line and */ at the end of the last line.

multilinedcmt.c
#include <stdio.h>

int main() {
  /* Printing Hello World! message on screen
  Telling my friends that I am fine */
  printf("Hello World!\n");
  printf("I am fine\n");
  return 0;
}


Some Points about comments :

  • If you do not want some lines of your code to execute, put them in comments.
  • To comment a line, click on that line to place cursor on that line and then press "Ctrl + /".
  • Comments are just used to increase the readability of your code.