• C Programming Video Tutorials

Comments in C



Using comments in a C program increases the readability of the code. You must intersperse the code with comments at appropriate places. As far as the compiler is concerned, the comments are ignored. In C, the comments are one or more lines of text, that the compiler skips while building the machine code.

The comments in C play an important part when the program needs to be modified, especially by somebody else other than those who have written it originally. Putting comments is often not given importance by the programmer, but using them effectively is important to improve the quality of the code.

Why to Use Comments in C Programming?

Any programming language, including C, is less verbose as compared to any human language like English. It has far less number of keywords, C being one of the smallest languages with only 32 keywords. Hence, the instructions in a C program can be difficult to understand, especially for someone with non-programming background.

The C language syntax also varies and is also complicated. Usually, the programmer tries to add complexity to optimize the code. However, it makes the code hard to understand. Comments provide a useful explanation and convey the intention behind the use of a particular approach.

For example, take the case of the ?: operator in C, which is a shortcut for the if-else statement.

So, instead of the following code −

if (a % 2 == 0){
   printf("%d is Even\n", a);
} else {
   printf("%d is Odd\n", a);
}

One can use the following statement −

(a % 2 == 0) ? printf("%d is Even\n", a) : printf("%d is Odd\n", a);

Obviously, the second method is more complicated than the first. If useful comments are added, it makes easier to understand the intention and logic of the statement used.

Types of Comments in C

In C, there are two types of comments −

  • Single-line comments
  • Multi-line comments

Single-line Comment in C

The C++-style single-line comments were incorporated in C compilers with C99 standards. If any text begins with the // symbol, the rest of the line is treated as a comment.

The text followed by a double oblique or forward slash [//] in a code is treated as a single-line comment. All that text after // is ignored by the C compiler during compilation. Unlike the multi-line or block comment, it need not be closed.

Syntax of Single-line C Comment

//comment text

The // symbol can appear anywhere. It indicates that all the text following it till the end of the line is a comment. The subsequent line in the editor is again a place to write a valid C statement.

Example: Single-line Comment in C

Take a look at the following program and observe how we have used single-line comments inside its main function −

/* Online C Compiler and Editor */
#include <stdio.h>
#include <math.h>

/*forward declaration of function*/
float area_of_square(float);

float area_of_square(float side){
   float area = pow(side,2);
   return area;
}

// main function - entire line is a comment
int main(){

   // variable declaration (this comment is after the C statement)
   float side = 5.50; 
     
   float area = area_of_square(side);   // calling a function
   printf ("Side = %5.2f Area = %5.2f", side, area);

   return 0;
}
Output

When you run this code, it will produce the following output −

Side = 5.50 Area = 30.25

Multi-line Comment in C

In early versions of C (ANSI C), any length of text put in between the symbols /* and */ is treated as a comment. The text may be spread across multiple lines in the code file. You may call it a multi-line comment. A block of consecutive lines is treated as a comment.

Syntax of Multi-line C Comment

The general structure of a multi-line comment is as follows −

/* The comment starts here
Line 1
Line 2
…..
…..
Comment ends here*/

For example −

/*
Example of a multi-line comment
program to print Hello World
using printf() function
*/

Obviously, since the comments are ignored by the compiler, the syntax rules of C language don't apply to the comment text.

Comments can appear anywhere in a program, at the top, in between the code, or at the beginning of a function or a struct declaration, etc.

Example: Multi-line Comment in C

In this example, we have a multi-line comment that explains the role of a particular user-defined function used in the given code −

/* program to calculate area of square */

/* headers */
#include <stdio.h>
#include <math.h>

/* forward declaration of function */
float area_of_square(float);


/* main function */
int main(){

   /* variable declaration */
   float side = 5.50;
   
   /* calling function */
   float area = area_of_square(side);
   printf("Side = %5.2f Area = %5.2f", side, area);

   return 0;
}

/* User-defined function to calculate
the area of square. It takes side as the argument 
and returns the area */

float area_of_square(float side){
   float area = pow(side, 2);
   return area;
}
Output

When you execute the code, it will produce the following output −

Side = 5.50 Area = 30.25

While inserting a comment, you must make sure that for every comment starting with /*, there must be a corresponding */ symbol. If you start a comment with /* and fail to close it, then the compiler will throw an error.

Note: A blocked comment or multi-line comment must be put inside /* and */ symbols, whereas a single-line comment starts with the // symbol and is effective till the end of the line.

Placing comments in a program is always encouraged. Programmers usually avoid the practice of adding comments. However, they can sometimes find it difficult to debug and modify their code if it is not properly commented. Comments are especially vital when the development is done in collaboration. Using comments effectively can be of help to all the members of a team.

Even though comments are ignored by the compiler, they should be clear in meaning and concise. Whenever the code needs modification, the reason, the timestamp, and the author should be mentioned in comments.

Advertisements