Programming in C

(Barry) #1

D Common Programming Mistakes


D Common Programming Mistakes


THE FOLLOWING LIST SUMMARIZES SOME OFthe more common programming mistakes
made in C.They are not arranged in any particular order. Knowledge of these mistakes
will hopefully help you avoid them in your own programs.


  1. Misplacing a semicolon.
    Example
    if ( j == 100 );
    j = 0;


In the previous statements, the value of jwill always be set to 0 due to the
misplaced semicolon after the closing parenthesis. Remember, this semicolon is
syntactically valid (it represents the null statement), and, therefore, no error is pro-
duced by the compiler.This same type of mistake is frequently made in whileand
forloops.


  1. Confusing the operator = with the operator ==.
    This mistake is usually made inside an if,while, or dostatement.
    Example
    if ( a = 2 )
    printf ("Your turn.\n");


The preceding statement is perfectly valid and has the effect of assigning 2 to a
and then executing the printfcall.The printffunction will alwaysbe called
because the value of the expression contained in the ifstatement will always be
nonzero. (Its value will be 2.)


  1. Omitting prototype declarations.
    Example
    result = squareRoot (2);


23 0672326663 AppD 6/10/04 2:00 PM Page 497

Free download pdf