INFO: Common Programming Errors in the C Language

Article translations Article translations
Article ID: 22321 - View products that this article applies to.
This article was previously published under Q22321
Expand all | Collapse all

SUMMARY

The text below lists some of the most common errors that occur programming in the C language. Any one of these items can cause unpredictable results, such as invalid data. Some are caught by the compiler and reported as errors or warnings.
  • Using an automatic variable that has not been initialized
  • Omitting a closing comment delimiter
  • Using an array index greater than the length of the array (In C, array indexes run from zero to <length>-1.)
  • Omitting a semicolon or a closing brace
  • Using an uninitialized pointer
  • Using a forward slash when a backslash is required (for example, substituting "/n" for "\n.")
  • Using "=" in a comparison where "==" is desired
  • Overwriting the null terminator for a string
  • Prematurely terminating a function declaration with a semicolon (The compiler often flags the "orphan" block of code as a syntax error.)
  • Specifying the values of variables in a scanf() statement instead of their addresses
  • Failing to declare the return type for a function
  • Assuming an expression evaluation order when using an expression with side effects (For example, a[i] = i++; is ambiguous and dangerous.)
  • Failing to account that a static variable in a function is initialized only once
  • Omitting a "break" from a case in a switch statement (Execution "falls through" to subsequent cases.)
  • Using "break" to exit a block of code associated with an if statement (The break statement exits a block of code associated with a for, switch, or while statement.)
  • Comparing a "char" variable against EOF (-1). The following idiom results in an infinite loop when char is unsigned. Note that char is signed by default, so the following will only fail when using the "/J" compiler option:
    char c;
    while ((c = getchar()) != EOF)
       {
       }
    						

Properties

Article ID: 22321 - Last Review: July 5, 2005 - Revision: 2.1
APPLIES TO
  • Microsoft C Professional Development System 6.0a
  • Microsoft Visual C++ 1.0 Professional Edition
  • Microsoft Visual C++ 1.5 Professional Edition
  • Microsoft Visual C++ 2.0 Professional Edition
  • Microsoft Visual C++ 4.0 Standard Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++, 32-bit Learning Edition 6.0
Keywords: 
kbinfo kblangc KB22321

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com