Previous: 4.5 Menu Driven Programs
Up: 4 Processing Character Data
Next: 4.7 Summary
Previous Page: 4.5 Menu Driven Programs
Next Page: 4.7 Summary

4.6 Common Errors

  1. Errors in program logic: The program does not produce the expected results during testing. Use conditional compilation to introduce debug statements.

  2. The value of getchar() is assigned to a char type. It should be assigned to a signed char type if it is to be checked for a possibly negative value of EOF.

  3. The keyboard buffer is not flushed of erroneous or unnecessary characters as explained in Section 4.4.

  4. Improper use of relational operators:
    if ('a' <= ch <= 'z')    /* should be ('a' <= ch && ch <= 'z') */
              ...
    The operators are evaluated left to right: 'a' <= ch is either True or False, i.e. 1 or 0. This value is compared with 'z' and the result is always True.

  5. An attempt is made to read past the end of the input file. If the standard input is the keyboard, it may or may not be possible to read input once the end of file keystroke is pressed. If the standard input is redirected, it is NOT possible to read beyond the end of file.

  6. A break statement is not used in a switch statement. When a case expression matches the switch expression, control passes to that case label and control flow continues until the end of the switch statement. The only way to terminate the flow is with a break statement. Here is an example:
    char find_next(char c)
         {    char next;
    

    switch(c) { case 'z': next = 'a'; default: next = c + 1; } return next; }

    Suppose c is 'z'. The variable next is assigned an 'a' and control passes to the next statement which assigns c + 1 to next. In fact, the function always returns c + 1 no matter what c is.

  7. Errors in defining macros. Define macros carefully with parentheses around macro formal parameters. If the actual argument in a macro call is an expression, it will be expanded correctly only if the macro is defined with parentheses around formal parameters.

  8. A header file is not included in each of the source files that use the prototypes and/or macros defined in it.

  9. Repeated inclusion of a header file in a source file. If the header file contains defines, there is no harm done. BUT, if the header file contains function prototypes, repeated inclusion is an attempt to redeclare functions, a compiler error.

  10. Failure to set environment parameters, such as the standard include file directory, standard library directory, and so forth. Most systems may already have the environment properly set, but that may not be true in personal computers. If necessary, make sure the environment is set correctly. Also, make sure that the compile and link commands correctly specify all the source files.



Previous: 4.5 Menu Driven Programs
Up: 4 Processing Character Data
Next: 4.7 Summary
Previous Page: 4.5 Menu Driven Programs
Next Page: 4.7 Summary

tep@wiliki.eng.hawaii.edu
Wed Aug 17 08:29:11 HST 1994