Comments in C++

Comments in C++

 

Here, in this section we will discuss about comments in  C++.Comments are used to insert meaningful description or notes where ever required in  the code so as to enhance  readability and user understanding of the program.

  • Comments are statements in the program that are ignored i.e not executed by the compiler
  • Comments can be used anywhere, any point, and any number of times in the program

comments in C++

Single Line Comments

They are used with 2 backslashes (//) everything after (//)is ignored by the compiler within that line

Program to demonstrate single Line comments

main()
{
int a=2,b=3//a,b are varible of type int
std::cout<<a+b;//cout is present in std namespace
}
  • Single line comments are always applicable in a line. if you don’t use a backslash in the next line they are treated as normal code and may result in an error
  • So you need to use //es in each line separately

    main()
    {
    cout<<"trishaank";//this will print trishaank
    this is not commented
    //this is commented
    }

O/P

complie time error

 

MultiLine Comments

 

  • The start with /* and end with */ .ie the block of code that falls between /* and */ is commented
  • They are also called as block comments

Program to demonstrate MultiLine comments

/* Multiline comments in C++*/
main()
{
cout<<"trishaank"/* this will print trishaank
cout is present in std:: namespace*/
}

Single line comments cannot be closed in between i.e once you use a single line  comment again you cannot write normal code within that line, whereas multiline comments  can be closed in between and again you can write code within the same line

Example

main()
{
cout<<"trishaank\t";/*normal_code_after*/cout<<"rishi;
//you caanot normal code in this line
}