C++ Comments

What is Comments in C++ ?

[one_third last=”no”]comment[/one_third] Comment in C++ Programming is similar as that of in C .Each and every language will provide this great feature which is used to document source code.We can create more readable and eye catching program structure using comments.We should use as many as comments in C++ program. Comment is non executable Statement in the C++.

Types of Comment in C++ Programming

We can have two types of comment in Programming -

  1. Single Line Comment
  2. Multiple Line Comment

Single Line Comments in C++

cout<<"Hello"; //Print Hello Word
cout<<"www.c4learn.com"; //Website
cout<<"Pritesh Taral"; //Author
  • Single Line comment starts with “//” symbol.
  • Remaining line after “//” symbol is ignored by browser.
  • End of Line is considered as End of the comment.

Multiple Line Comments in C++

int main()
{
/* this comment
        can be considered
           as 
   multiple line comment */
cout << "Hello C++ Programming";
return(0);
}
  • Multi Line comment starts with “/*” symbol.
  • Multi Line comment ends with “*/” symbol.

How to use Comment more efficiently in C++ Programming

  1. Comment for User Documentation.
  2. Hiding Non-usable Code.
  3. Hiding Single Statement.
  4. Comment used to explain particular language statement

1. Comment used for Documentation

/* Name : Main Function
 * Parameter : None
 * Return Value : Integer
 */
int main()
{
cout << "Hello C++ Programming";
return(0);
}

Above comment is used for documentation. We use This type of Documentation to summarize C++ code.

2. Comment used to Hide Non-usable line.

<script type="text/javascript">
// document.write("<h1>Heading</h1>");
document.write("<p>Google</p>");
document.write("<p>Yahoo</p>");
</script>

We have used comment to prevent execution of single line.

// document.write("<h1>Heading</h1>");

3. Comment used to Hide Non-usable Multiline Code.

int main()
{
person p1,p2;
p1.getData();
p2.putData();
// p3.getData();
return(0);
}

4. Comment written after Statement

int main()
{
person p1,p2;
p1.getData(); // Get Personal Info
p2.putData(); // Save Personal Info
return(0);
}

Something About Comments :

  1. Comments are non Executable Statements.
  2. Comments are always neglected by compiler.
  3. Comments are replaced by white spaces during the preprocessor phase.
  4. Comments can be written anywhere and any number of times.
  5. Comments can be used for documentation.
  6. Comments can be Single Line or multiple line.

Bad Habits of Using Comments in C++ :

  1. Don’t Write Comment for statements that can be easily understood .
int main()
{
int age=0;
//initialize age to 0
return(0);
}

Read More : Wiki Article on Comment |