C Comments

Description

The comment is used to add description to the source code. Sometime we can use comment to hide part of the source code too.

Comments in a C program have a starting point and an ending point. Everything between those two points is ignored by the compiler.

Syntax

We can use the following syntax to add comments to C program.

/* comment */

or

// comment

Note

/* comment */ is often used to multi-line comment, while // is usually used for single line comment.

Example 1

The following code has a line of comments placed before printf statement.


#include <stdio.h>
 //from ww  w . jav a  2 s .  c o m
int main(){
   /* This is the comment.*/
   printf("%15s","right\n");
   printf("%-15s","left\n");
   return(0);
}

The code above generates the following result.

Example 2

Comments are ignored by the compiler. We can use comments to disable certain parts of your program.


#include <stdio.h>
 /*w  ww . jav  a2 s  .co m*/
int main(){
   /* printf("%15s","right\n"); */
   printf("%-15s","left\n");
   return(0);
}

The code above generates the following result.

Example 3

The '//' is used as single line comment.


#include <stdio.h>
main(){//from w w w. j  a  va2s. c o  m
    int i,j,k;
   
    i = 6;    
    j = 8;
    k = i + j;
   
    printf("sum of two numbers is %d \n",k); // end of line comment
}

The code above generates the following result.


sum of two numbers is 14

Example 4

At the beginning of the program is a comment block. The comment block contains information about the program. Boxing the comments makes them stand out.

The some of the sections as follows should be included.

  • Heading.
  • Author.
  • Purpose.
  • Usage.
  • References.
  • File formats. A short description of the formats which will be used in the program.
  • Restrictions.
  • Revision history.
  • Error handling.
  • Notes. Include special comments or other information that has not already been covered.

/********************************************************           
 * hello -- program to print out "Hello World".         *   
 *                                                      *   
 * Author:  FirstName, LastName                         *   
 *                                                      *   
 * Purpose:  Demonstration of a simple program.         *   
 *                                                      *   
 * Usage:                                               *   
 *      Runs the program and the message appears.       *   
 ********************************************************/  
//ww w . j a  va  2 s .  com
 #include <stdio.h> 
 int main() 
 {  

     /* Tell the world hello */ 
     printf("Hello World\n");   
     return (0);
 }  

The code above generates the following result.





















Home »
  C Language »
    Language Basics »




C Language Basics
Data Types
Operator
Statement
Array