You are here:   HomeC Programming (Theory)Basic Of C Prog.Comments in C
by Dinesh Thakur Category: Basic Of C Programming

If you construct a long program today and try to recall it after a few days, there is every possibility that you would have forgotten quite a few things and even what the program does or what a particular name stands for. Comments if included in a program help in recollection of different names, data values and other parameters.

Generally, a comment is enclosed between the symbols /* at the start and *I at the end. A comment may be of any length. It may be spread over several lines, generally referred to as block comment, or it may be included in a code line or written separately. Following are some examples of comments:

/* This is my first comment.*/

A= S/* Sale amount*// N;

"Delhi/**/London"

The second line in the above example is equivalent to the code A = S/N; and Sale amount is a comment enclosed between I * and * I. In the third line, I * *I is not a comment in the program, instead it is part of a character string that is enclosed between the double quotes (" ") . Including comments between the symbols/* and */ is useful for part-of-line comment as well as for block comment. Remember that there is no space between the two characters in/* or */.

The C++ style of the comment is also now part of C language. In this style, a comment may be put after double slash (//) up to the end of line. If it is to be used within a code line, then it should start after the code because any code written after double slash will become part of comment. If the comment is long and goes to next line, then another double slash is needed at the beginning of next line. An example of this is given in below

Illustrates inclusion of comments. The code lines are shown in bold.

#include <stdio.h>

// This is /*my*/ comments program.
void main() //The program illustrates comments.
{
   /* This is my //first//program with comments*/
  printf (/*A comment*/"Welcome to //programming /* See This Comment */ in C.\n");
  /*This is a block comment within a code line which end with
   * semicolon.
   * Some prefer to put asterisk before every line.
   * This is another comment.
   */;
}

The expected output of the above program is as follows:

        Comments in C

In the above output, the comment included in the output string [i.e., the statement between double quotes and argument of printf()] has not been neglected and has become part of the output, whereas all other comments have been neglected. Also, note that you cannot have any code between/* and*/, because it would become part of comment. Remember that the comment symbols/* and*/ cannot be nested.



Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article