C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1
need faster. If you didn’t comment, you would have to decipher your C code every time
you looked through a piece of it.

Program maintenance is the process of changing a program over time to fix hidden bugs and to adapt
the program to a changing environment. If you write a payroll program for a company, that company
could eventually change the way it does payroll (to go from biweekly to weekly, as an example), and
you (or another programmer) will have to modify the payroll program to conform to the company’s
new payroll procedures. Commenting speeds program maintenance. With comments, you or another
programmer can quickly scan through a program listing to find the areas that need changing.


Comments are not C commands. C ignores every comment in your program. Comments are for people,
and the programming statements residing outside the comments are for the computer.


Consider the following C statement:


return ((s1 < s2)? s1 : s2);

You don’t know C yet, but even if you did, this statement takes some study to figure out. Isn’t this
better?


Click here to view code image


return ((s1 < s2)? s1 : s2); /* Gets the smaller of 2 values */

The next section explains the syntax of comments, but for now, you can see that the message between
the / and the / is a comment.


The closer a comment is to spoken language and the further a comment is from C code, the better the
comment is. Don’t write a comment just for the sake of commenting. The following statement’s
comment is useless:


Click here to view code image


printf("Payroll"); /* Prints the word "Payroll" */

Warning

You don’t know C yet, and you still don’t need the preceding line’s comment!
Redundant comments are a waste of your time, and they don’t add anything to
programs. Add comments to explain what is going on to people (including yourself)
who might need to read your program.

Specifying Comments


C comments begin with / and end with /. Comments can span several lines in a program, and they
can go just about anywhere in a program. All of the following lines contain C comments:


Click here to view code image


/* This is a comment that happens to span two lines
before coming to an end */
Free download pdf