Comments in cpp 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 cpp programming language we have two type of comments.

1. Single Line Comment

2. Multi Line Comment


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 cpp compiler

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

printf("Any Message!"); // This is a single line comment

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 cpp language.
Any text between /* and */ will be ignored by the cpp 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 cpp 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.