12 Common C Errors

In this list I'll cover 12 common pitfalls that are made in C. The check-list is meant as a tool for beginners.

 if (number = 9) number = 0;  while (ch = getchar != EOF)  while ((ch = getchar) != EOF)  int myArray[10];  for (i=0; i <= 10; i++) myArray[i] = i; x = square;   /* incorrect */ x = square; /* correct */  void convert(int *px);  int result; convert(&result);</tt>  long a; a = getlong;</tt>  /* The void is not necessary, but good practice */ long getlong(void); long a;</tt>  long a, getlong(void);</tt> switch (weekday) { case MONDAY : printf("Start a new week"); case TUESDAY:        /* no break here */ case FRIDAY : workday++; break; default: weekday++; }</tt> a[n] = n++;</tt> a[n] = n; n = n + 1;</tt> a[n + 1] = n; n = n + 1;</tt> char line[80]; ch = *lp++;        /* the same as *(lp++) */</tt> ch = (*lp)++;</tt> This article is based on material collected by Finn Overgaard Hansen DTI 1993.
 * 1) Remember to start and end comments correctly
 * Comment begin: /*
 * Comment end: */
 * 1) The assignment operator '=' is often mistaken for the test operator '==' 
 * This mistake is easy to make since many other programming languages use '=' as a test operator. The compiler may not discover this type of error since the assignment has a value and therefore is legal in an expression. An example is:
 * 'number' is assigned the value 0 since the 'number = 9' assignment always returns a true value.
 * 1) Errors with assignments in statements
 * Missing brackets around assignments causes errors that the compiler wont be able to find.
 * Example:
 * 'ch' will here be assigned the value 1 for TRUE until 'getchar' returns EOF, where 'ch' is assigned the value 0 for FALSE. The correct statement:
 * 1) Errors with arrays
 * In C the lowest array-index is zero, i.e. the following declaration:
 * sets up 10 elements myArray[0], myArray[1], ..., myArray[9]. Example:
 * Since C doesn't have runtime-checks of array boundaries, this example will cause the variable myArray[10] to be assigned a value. But since myArray[10] hasn't been declared another variable will be overwritten. [...If there is another variable in memory right after the array. Lack of bound checking is the cause of many security holes in networking software. Ed].
 * 1) Remember brackets when calling a function without parameters
 * Example:
 * and
 * Note: Depending on what compiler you have you will get a compiler error from the line above.
 * 1) Errors by transferring a pointer to a function
 * If the function 'convert' is declared as
 * where the parameter is a pointer to an integer, the function has to be called with a pointer as parameter. Example:
 * The typical error is caused by omission of the address operator '&'
 * 1) Remember to declare functions
 * If functions returns other values than the 'int' type, they have to be declared before they are called. Example:
 * In this case 'a' is assigned an 'int' instead of a 'long'. In order to be correct the function has to be declared like this (also called prototyped):
 * could also be declared like this [confusing style though. Ed]:
 * 1) Remember 'break' in the 'switch' construct
 * If you forget a 'break', the program will continue until it meets another 'break' or the end of the 'switch' block has been reached. Example:
 * If you start with MONDAY you will also execute the TUESDAY and FRIDAY code because there is no break until after FRIDAY, which means that default wont be executed unless weekday is different from MONDAY, TUESDAY or FRIDAY.
 * If there are statements executed with several entrances like in the example above, and break is left out on purpose, it should be commented.
 * 1) Use of side effects in expressions
 * The use of side effects in expressions can cause unpredictable programs. Example:
 * is equivalent either to:
 * or to:
 * Which case holds depends on the compiler.
 * 1) Errors with type conversions
 * Type conversion of a 'char' to an 'int' or a right shift of an 'int' can be done with or without a 'sign extension'.
 * [Which happens depends on how the compiler is implemented. Ed]
 * 1) Remember brackets with pointers and increment/decrement operators
 * Example:
 * lp = line;
 * In this example 'ch' is assigned what 'lp' is pointing at after which lp is incremented.
 * In this example 'ch' is assigned what 'lp' is pointing at, but in this case it's the value that 'lp' is pointing at that's incremented.
 * 1) Watch out that you don't confuse the logical operators (&&, ||) with the bit operators (&, |)