File vs keyboard/screen input-output functions

File Stdio (i.e. the keyboard and screen)
ch=getc(fp) ch=getchar()
putc(ch,fp) putchar()
fprintf() printf()
fscanf() scanf()
fgets(string, length,fp) gets()
fputs(string,fp) puts()

where:

FILE *fp
char ch
char *string


General causes of bugs

  1. Failure to initialize
  2. Array bounds overflow -- this has very unpredictable results (including crashing the machine)
  3. Failure to anticipate a logical combination on a branch. This is particularly problematic when you are dealing with input files
  4. "Off-by-one" errors -- program works except for the first or last iteration
  5. Algorithm/equation is incorrect in the first place
  6. Copying some code and forgetting to change a variable name
  7. Mixing local and global variables; side-effects from procedures. More generally, believing a variable has a different value than it has.

General advice on debugging:

  1. Compartmentalize: make sure your procedures work before combining them into programs.
  2. Test everything as you go along, and use a debugger
  3. Insert print statements everywhere to verify the values of variables when you don't know what is going on (or use a debugger)
  4. Read the code very, very carefully

Priorities:

  1. Make it run -- eliminate syntactic errors and anything else that crashes the program
  2. Make it right -- be sure the program is doing what you want it to do
  3. Make it rapid -- optimize the code if necessary (for most applications you will write, this won't be necessary)