Tuesday, 17 September 2013

Comments in C++


In this c tutorial for beginners, we are going to discuss about how to write a comment??
Comments is a kind of tool which is used to highlight the specific details and thus, not making it a part of C++ program. Comments are used so that the program you write should be clear not only to you but also to the reader. It is a good programming practice that you include comments to your program to make it clear for the readers.  Comments are for the readers not for the compiler.

Single line commenting:

For the commenting of single line, “\\” is used.
e.g:  //This is a single line comment

Multiple lines commenting:

For the multiple lines commenting, “/* */” is used.
e.g:  /* You can include
                comments that can
       occupy multiple lines  */
Uses of comments:

· Comments used to give reference in the start of the program.

Comments are used to identify the authors of the program, dates on which it is written or the name of organization who wrote it.

Example:
// Written by Tabish Habib
// Written on 1st January 2013
//Written by shapesinc.blogspot.com

·  Comments used to give heading or explanation:

Comments are used to give the heading of the program or brief explanation about the program.

Example:
// Diamond shape
/* This is a generic program to print diamond shape. In this program, we
take a size and with the used of two nested loops, we print a diamond */

·  Step by step commenting:

We use comments to explain each and every step of the program.

Example:
int num=0;    //an int variable num is declared with a zero value assigned
char word=’A’;   //a char variable word is declared with an A assigned
float point=2.2;  //a float variable point is declared with a value 2.2 assigned
bool check=false;  //a bool variable check is declared with a false  assigned

·  Keeping unnecessary code in the program:

If you want to keep the code in the program but don’t want to execute it , then you should comment that code with multiple commenting. This will do the work because commenting is not for the compiler so, when the compiler compiles the program then it doesn’t consider the part that is commented. Compiler ignores everything that appears within comments.


Keyboard short key for commenting:

If you want to comment any part then select that part of code and then first press ctrl+k and then ctrl+c.

Key: Ctrl+K, Crtl+C


Keyboard short key for uncommenting:

If you want to uncomment any part then select that part of code and then first press ctrl+k and then ctrl+u.
Key: Ctrl+K, Crtl+U

Final Words:

In this post of shapes in c, you have learnt the meaning, use and advantages of comments. In this c tutorial for beginners, we discussed the topic of C++ commenting in detail and each and every aspect of how to write a comment.

0 comments:

Post a Comment