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;
switch(x) {
case 1:
printf("1\n");
case 2:
printf("2\n");
case 3:
printf("3\n");
}
9. Size of arrays
int a[10];
10. Integer division
double aHalf = 1/2;
float aHalf = 1.0/2;
double aHalf = 1.0/2;
11. Loop errors
int n = 10;
while(n>0);
x--;
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
char str[30];
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.
Gg1