Friday, January 16, 2009

How Do We Add Comments In C

can be added to a C program.

/* This is a comment ignored by the compiler */
main( ) /* This is another comment ignored by the compiler */
{
printf("We are looking at how comments are "); /* A comment is
allowed to be
continued on
another line */
printf("used in C.\n");
}
/* One more comment for effect */

Comments are added to make a program more readable to you but the compiler must ignore the
comments. The slash star combination is used inC for comment delimiters. They are illustrated
in the program at hand. Please note that the program does not illustrate good commenting
practice, but is intended to illustrate where comments can go in a program. It is a very sloppy
looking program.

The first slash star combination introduces the first comment and the star at the end of the first
line terminates this comment. Note that this comment is prior to the beginning of the program
illustrating that a comment can precede the program itself. Good programming practice would
include a comment prior to the program with a short introductory description of the program.
The next comment is after the "main( )" program entry point and prior to the opening brace for
the program code itself

The third comment starts after the first executable statement and continue for four lines. This
is perfectly legal because a comment can continue for as many lines as desired until it is terminated. Note carefully that if anything were included in the blank spaces to the left of the three continuation lines of the comment, it would be part of the comment and would not be compiled. The last comment is located following the completion of the program, illustrating that comments can go nearly anywhere in a C program.

Experiment with this program be adding comments in other places to see what will happen.
Comment out one of the printf statements by putting comment delimiters both before and after
it and see that it does not get printed out.

Comments are very important in any programming language because you will soon forget what
you did and why you did it. It will be much easier to modify or fix a well commented program
a year from now than one with few or no comments. You will very quickly develop your own
personal style of commenting.

Somecompilers allow you to "nest" comments which can be very handy if you need to "comment
out" a section of code during debugging. Check your compiler documentation for the availability
of this feature with your particular compiler. Compile and run comments.c at this time.

Google Search

Custom Search