Skip to main content

C++ Comments

Comment is a short useful description and message that are describe the information about logic or operation of statements. And this are provide readability of source code of programmer. Compiler are ignores comment portion and also not execute. those are useful only of programmer.

Why use comments?

Suppose that we are given thousand line of code without any comment. and we a re need to change only few operation of this code. Because code of not commented then we are trace every operation manually. This process take more time. so comments are used in this situation to understand our code to another programmer easily. and comment are indicates good programmer habit.

C++ are support two type of comments. single line and multi-line comments.

Single line comment

single line comment are start with // (two forward slash). Single line comment are used to short and small description. syntax of single comment.

//user comment here

View the example of single line comment.

//this program are calculate sum of two integers
#include<iostream>
using  namespace std;
int main(){
  //variable declaration
  int num1=20,num2=10;
  
  //display the sum of two integer number
  cout<<"Sum ("<<num1<<"+"<<num2<<") :"<<num1+num2;
  return 0;
  //end of execution of program 
}
Output
Sum (20+10) :30

In this program given single line comments. this are not display in output of program. Note that there are possible to continue single line to next line. so we need to use one (/) backslash. see the demo.

//this program display simple \
message like "Welcome to programming"
#include<iostream>
using  namespace std;
int main(){
    //display simple\
    message to user
  cout<<"Welcome to programming";
  return 0;
  //end of execution of program 
}
Output
Welcome to programming

single line comment are good but there are limit it will work on single line and use next line we are need to use backslash. In this case multiline comment are suitable for long text.

Multi-line Comments

If we need to describe more information of relevant code then multi-line is suitable. this is start with (backslash star symbol) /* and end with */ (star backslash). see example.

/*
  Include header file in this section.
  This is mutiline example
*/
int main(){
    /*
      Compiler are start the program execution of main function.
    They are execute statement and user defined function.
    */
  return 0;
}

Important point

1) inside a multi-line comment compiler is ignored text symbols and operator. understand this point using this example.

/*
  include header file
*/
#include <iostream>

/*----------
* Function : sum
* Accept   : two float value
* return   : return addition of two given number 
*-----------*/
float sum(float value1,float value2){
  return value1+value2;
}
using namespace std;
int main(){
  //declare two float variable
  float value1=20,value2=80;
    cout<<"Sum is :"<<sum(value1,value2);
  return 0;
}
Output
Sum is :100

2) comment can use to separate line and after the statement. inside a statement it will not suitable.

#include <iostream>
using namespace std;
int main(){//start program execution
  int status=10;
  /*
      'if(status>5){ ' is statement we are use comment after this
  */
  if(status>5){ //when status variable is greater than 5
    //code
  }else{
    //code
  }
  return 0; //end of program
}

Normally comments are used in all programming language. because this can provide understandability and readability of source code.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment