This article continues the one published two weeks ago: Top 101 mistakes done by novice programmers C and Top 101 mistakes done by novice programmers C – part 2
8. Forgetting to put a break in a switch statement
Remember that C does not break out of a switch statement if a case is encountered. For example:
int x = 1;
9. Size of arrays
10. Integer division
double aHalf = 1/2;
float aHalf = 1.0/2;
double aHalf = 1.0/2;
11. Loop errors
int n = 10;
12. Null terminated strings
12.a Not null terminating strings
C assumes that a string is a character array with a terminating null character. This null character has ASCII value 0 and can be represented as just 0 or '\0'. This value is used to mark the end of meaningful data in the string. If this value is missing, many C string functions will keep processing data past the end of the meaningful data and often past the end of the character array itself until it happens to find a zero byte in memory!
12.b Not leaving room for the null terminator
A C string must have a null terminator at the end of the meaningful data in the string. A common mistake is to not allocate room for this extra character. For example, the string defined below
only has room for only 29 (not 30) actually data characters, since a null must appear after the last data character.
Part four in the next fifteen days.