Common 'C' Programming Errors
Getting your programs to Compile:
  1. End all statements in a semi-colon, except for your branching if and while statements.
  2. Make sure you have all the necessary header files. e.g. may need math.h or stdlib.h.
  3. Make sure all the braces { } and () match appropriately.
  4. Make sure all your variable names are spelt correctly.
Unfortunately 'C' doesn't give compiler warnings or errors to code that obviously not what the programmer intended. So even when your program compile you may have "weird" bugs.
  1. Saying            " if (a = b) {"      instead of "if (a == b) {"        // understand what happens in the first case
  2. Having a semi-colon at the end of a for loop   "for (i=0; i<10; i++);"    // this loop spins and just changes i to 10.
  3. Not having { } around code that should execute together.  Avoid this by always using { }, even for one line of code.

  4. if (a == b)
        a = 10;
    else
        a = 11;        // this line executive is a not equal to b.
        b = 12;        //  this line will always be executed


Ways to avoid errors
  1. Plan out program logic before you start coding.
  2. Pay attention to your variable names make them meaningful.
  3. Pay attention to white space, especially proper indentation.
  4. Compile your program often, fixing syntax and logic errors as you write your program.
  5. Don't copy code from your neighbours, not only may you get zero for cheating, but you won't understand what you are trying to do. This will hurt you a great deal on tests and exams.
  6. Use printf statements for debugging as you write code before you know you will need them.
  7. Test code as you go. Try to test the different logic paths, think of test cases before you write code.
  8. Learn how to use the debugger.
Computer Studies Home                       ICS3M  Home               Top