0.8 – Commentations in C language

by subbu on July 23, 2013

Writing comments in C language:

It is one or more lines of text inserted in the source code to explain what, why and how the code is written. The text written as comment is ignored by the compiler. Comments give an idea that what actually the code is doing without reading the actual code. It will be helpful for quick reference in future. It is so important to properly comment the code when a team of people are involved in development.

Sometimes programmer wants to solve a problem in different ways, in such case a part of code can be commented to try in another way. In future it will remind you why you have selected another way in solving problem.

In C language we write the comment within /*   and  */ which will be ignored by the C compiler. Make sure that comments must not be nested that is a comment can’t be placed within another comment.

 Example:

int fir, sec, sm;
fir=45;
sec=30;
sm=fir+sec;
printf("Sum of two numbers %d",sm);

The same code can be written with proper comments in the following way. Here program level comments, section level comments and statement level comments are used for easy understanding.

/* Demonstration of
declaration, assigning and output statements */

/* declaration of variables */
int fir, sec, sm;   /*declared three integer variables*/

/* assigning statements */
fir=45;             /*Assigning 45 to fir */
sec=30;           /* Assigning 30 to sec*/
sm=fir+sec;     /* Assigning addition of fir and sec to sm */

/* Output statement */
printf("Sum of two numbers %d",sm);  /* Printing the output onto console */

Comments in ANSI C11:

C11 supports another way to comment a single line that is //. The text written after the symbol // until the end of line will be omitted by the C compiler. It is generally used for section level and statement level comments.

The above code can be written in C11 in the following way

/* Demonstration of
declaration, assigning and output statements */

//declaration of variables
int fir, sec, sm;   //declared three integer variables

//assigning statements
fir=45;             //Assigning 45 to fir
sec=30;           //Assigning 30 to sec
sm=fir+sec;     //Assigning addition of fir and sec to sm

//Output statement
printf("Sum of two numbers %d",sm);  //Printing the output onto console

It eases the section and statement level comments but earlier versions of ANSI C compilers may not support single line comments (//)

 Summery:

C supports two comments that are

  1.  Multi line comments ( /*     */)
  2.  Single line comments ( // )

 Rules in writing comments:

  • Any number of comments can be written
  • Comments can be written any where
  • Comments can’t be nested
/* comment must be written within /* ... */ in C language */

The above statement is not legal because comments are nested

sm=fir /*value of fir */ + sec /* value of sec */;

Here the above statement executes properly because compiler just omit the text written in comments

printf("Sum of two numbers %d /* printing value of sm */", sm);

Output:

Sum of two numbers 75 /* printing value of sm */

Here we get some thing we don’t expect because compiler never read the text in format string and will be printed as it is.

Previous post:

Next post: