
160 Intermediate C Programming
fprintf ( outfptr , "% d\n ", count );49
// close the input file50
fclose ( infptr ) ;51
// close outupt file52
fclose ( outfptr );53
return EXIT _SUCCES S ;54
}55
Lines 41 and 45 implement the two definitions. Line 41 increments the address by only
one and searches again. Line 45 increments the address by length of the string. So “eyeye”
contains only one “eye”, because after finding the first “eye”, the program continues its
search from “ye”.
11.4 How to Comment Code
Almost every programming class requires that students comment their code. Addition-
ally, almost every programming book tells readers to comment their code. However, very
few classes or books say how to comment code. Writing comments is like writing an article
and it is difficult to grade comments. Comments are about communicating with the readers
of the code: Style and clarity are important. If comments do not explain code, then they are
not useful. It is not yet possible to check comments’ usefulness by using computer programs.
Grading comments by human eyes (usually by teaching assistants) is labor-intense. As a
result, some professors do not consider comments in grading and most students do not take
comments seriously. Sometimes students even write comments like “because the professors
tell us so.” Some students ask me, “Do I need to write comments for you?” My answer is,
“You need to write comments for yourself.” If your program is longer than, say, 20 lines,
you need to write comments before writing code.
This book frequently lists the steps before writing a program. These steps should be
written in the comments of the code. Remember that programs are written to solve prob-
lems. The programs implement solutions. The solution must be known before the first line
of code is typed. Writing a program without a solution first is like laying bricks for a house
before knowing how many rooms the house will have. Almost everyone agrees that a house
needs to be designed before it is built. Write code after you know the solution. It is good
practice to think about the solution, write down the steps, explain your thinking process in
comments.
In addition to explaining the steps, comments are needed to explain specifics of how
functions work: what is required of the arguments, and what the return result means.
Manual pages are good examples of this. Consider the manual page for fgets:
char *fgets(char *s, int size, FILE *stream);
fgets() reads in at most one less than size characters from
stream and stores them into the buffer pointed to by s. Reading
stops after an EOF or a newline. If a newline is read, it is stored
into the buffer. A terminating null byte (’\0’) is stored after the
last character in the buffer.
fgets() return s on success, and NULL on error or when end of
file occurs while no characters have been read.