Comment


  • comments is used to tell why the particular piece of code is written. it is added with the purpose of making the source code easier to understand.

  • comments should be used to explain “Why” a piece of code is written, not “How” it is done.

Note: Comments are ignored by the compiler. i.e. Compiler does not compile commented part


Two types of comment

  1. Single Line Comments :

    • Single line comments start with double slash(//).

    • Any text written after // will be ignored by the compiler(it will not be compiled).

    • Let’s see an example of single line comment.

    • Example 1: Added before the statement.
      	
      	#include<stdio.h>
      		void main(){
      			printf("Uncommented line");
      			//printf("Commented Line");
      		}
      	
      	
      	

      Output :

      	Uncommented line
      	
  2. Multi Line Comments:

    • Multi line comments can be used to add single as well as multi line comments. So, it is more convenient as compared to Single line comment.

    • It starts with a forward slash with asterisk(/*) and closes with asterisk with forward slash(*/). Let’s see an example:
      	#include<stdio.h>
      		void main(){
      			/* printf("Comment line 1");
      			   printf("Comment line 2");
      			   printf("Comment line 3");  
      			*/
      			  printf("Uncommented line");
      			  
      		}
          

      Output :

      	Uncommented line
      	

Use of comments

  • Anyway in my opinion we all agree with each other that software spend only 10% time of its life in development and rest of 90% in maintenance. This 90% part of maintaining the code is where comment can help you immensely.

  • Since no single developer stays till whole life of any product or software and its often new people, who works of already written code.

  • These are the people who read the code and not aware of why a certain piece of code has been written, here comments can help them to understand code quickly and believe me you will get lot of roses from that fellow developer .

    • Print Page