C++ Comments
Table of content
What is Comments in C++ ?
[one_third last=”no”]Types of Comment in C++ Programming
We can have two types of comment in Programming -
- Single Line Comment
- 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
- Comment for User Documentation.
- Hiding Non-usable Code.
- Hiding Single Statement.
- 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 :
- Comments are non Executable Statements.
- Comments are always neglected by compiler.
- Comments are replaced by white spaces during the preprocessor phase.
- Comments can be written anywhere and any number of times.
- Comments can be used for documentation.
- Comments can be Single Line or multiple line.
Bad Habits of Using Comments in C++ :
- 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 |