Comments in c programming language





Comments are those which hide the text from compiler. which is used to discribe the code or to create the documentation of your program in code.

Type of Comments


In C programming language we have two type of comments.
  • Single Line Comment
  • Multi Line Comment

1. Single Line Comment:

  • Single line comments are used to hide single line of code or text from compiler.
  • Single-line comments start with two forward slashes (//).
  • Any text after // will be ignored by the c compiler

Syntex:
printf("Hello World!"); // This is a single line comment
printf("Any Message!"); // This is a single line comment

2. Multi Line Comment:

  • Multi-line comments are used to hide group or block of text from compiler.
  • Multi-line comments start with /* and ends with */ in c language.
  • Any text between /* and */ will be ignored by the c compiler

Syntex:

/* here we have a code written below will print the Message
to the screen, and this is amazing use of multiline comment */
printf("Message!");

should i use single line comment or multiline comment?

It's up to developer which one he wants to use. Normally, we use // for short comments, and /* */ for longer one.

Few things that must remember while writing comments in C programing:


  • Text of Comment in the program should be enclosed within /* anything */ or //anything.
  • Unuseful lines of our code we can hide using comment.
    /* Text of Comment write here */
    si = p * n * r / 100 ;
  • we can write any number of comments at any place of the program.
    • Before the statement
      /* Comment */ si = p * n * r / 100 ;
    • After the statement
      si = p * n * r / 100 ; /* Comment */
    • Within the statement
      si = p * n * r / /* Comment */ 100 ;
  • Comments can't be nested means one comment cannot be written inside another comment.
    For example
    /* Cal of SI /* Nested Comment */ */ is invalid.