Scholarly Resources for CompSci Undergrads

C Programming

Common Mistakes & Marking Codes

This is a list of mistakes commonly seen in undergraduate computer science programming assignments. It is based on a list of marking codes by an anonymous author. I've changed some of the wording and added some more explanation to some of the points. There are separate sections of problems specific to C programs and Pascal programs.

A revised version that was used at Bowling Green State U. is also available in PostScript, and PDF formats. I've also got an XHTML version but it doesn't look right on today's browsers.

  1. Use meaningful variable and subprogram names.
    In C subprograms are all functions. In Pascal procedures and functions are subprograms.
  2. Comment on what your variables represent.
  3. Comment briefly on what your program does. Also comment what each subprogram does.
  4. Comment the `logical sections' of your code. Be brief but precise. Do not just restate what the source code says.
  5. Indent consistently for readability. Seperate sections of your program with blank lines.
  6. Do not unnecessarily repeat code in a program. Implement repeated tasks as subprograms or loops.
  7. Use modularity: the main routine should mainly just call the procedures that are the sub-modules of the program.
  8. Prompts to the user should be explicit, and helpful to the user.
  9. Format your output for readability.
  10. Submit all your output. Test runs should be executed in the order presented in the assignment.
  11. Do not use unnecessary parameters.
  12. Use parameters instead of global variables to pass information to and from subprograms.
    We will never give you an assignment where global variables are appropriate.
  13. Use the appropriate loop structure (e.g. post-test instead of pre-test).
    In C the loops types are: while (pre-test), do ... while (post-test), and for (counted pre-test). Pascal loops have the same names as C loops except that the post-test loop is repeat ... until.
  14. Do not include unnecessary code.
  15. Check your spelling.
  16. Use appropriate conditional structures.
  17. Avoid `magic numbers'. Use meaningfully named constants.
    In Pascal use const. In C use #define or typedef (but note that typedef is only for ints).

Note: Comments specific to Pascal programs are below.

Comments Specific to C

  1. If you use the gcc compiler then be sure that it is running as a standard C compiler and not a GNU C compiler.
    • gcc -ansi -pedantic makes it run as a standard C compiler
    • gcc -ansi -pedantic -Wall -O makes it run as a standard C compiler and check for several types of errors
    I recommend using a shell alias (e.g. `alias c89 gcc -ansi -pedantic -Wall -O' for csh) or a Makefile with gcc so that you always have it running the way you want.
  2. // is not (yet) a valid C comment.
  3. You should never use gets().
    Learn to use fgets() instead.
  4. Include appropriate library headers. You don't have to cast calls to malloc().
  5. main() must return an int.
  6. Declare proper prototypes for your functions.
    foo(); is not a proper prototype. Prototypes should declare a function's parameters and type.

Comments Specific to Pascal

  1. Use the appropriate kindof subprogram: a function if only one value is to be returned, a procedure otherwise.

See Also


http://www.csd.uwo.ca/~jamie/C/Mistakes/mistakes.html

About this document

Owner
J. Blustein
Created
23 Feb 1998 by J. Blustein
Last updated
12 October 2000 by J. Blustein