When looking at a program with errors, it can be difficult to know how to fix it. Through my studies of C, I have compiled a list of common mistakes made during C programming for reference.
1. Ignoring the case sensitivity of identifiers.
main()
{
int a=5;
printf(“%d”,A);
}
The compiler treats ‘a’ and ‘A’ as two different variable names, resulting in an error message. C treats uppercase and lowercase letters as different characters. By convention, constant names are written in uppercase and variable names in lowercase for better readability.
2. Ignoring variable types and performing illegal operations.
main()
{
float a,b;
printf(“%d”,a%b);
}
The ‘%’ operator is for modulus, which gives the integer remainder of ‘a/b’. Integer variables ‘a’ and ‘b’ can be used with the modulus operator, while floating-point variables cannot.
3. Confusing character constants with string constants.
char c;
c=”a”;
This confuses character constants with string constants. A character constant is a single character enclosed in single quotes, while a string constant is a sequence of characters enclosed in double quotes. C uses ‘\0’ as the string terminator, which is added automatically by the system, so the string “a” actually contains two characters: ‘a’ and ‘\0’, which cannot be assigned to a character variable.
4. Ignoring the difference between ‘=’ and ‘==’.
In many high-level languages, ‘=’ is used as the equality operator. For example, in BASIC you can write:
if (a=3) then …
But in C, ‘=’ is the assignment operator, and ‘==’ is the equality operator. For example:
if (a==3) a=b;
The former compares whether ‘a’ is equal to 3, while the latter assigns the value of ‘b’ to ‘a’ if ‘a’ equals 3. Beginners often make this mistake due to habitual issues.
5. Forgetting to add a semicolon.
A semicolon is an essential part of C statements, and must be placed at the end of statements.
a=1
b=2
During compilation, if the compiler does not find a semicolon after “a=1”, it treats the next line “b=2” as part of the previous statement, leading to a syntax error. When correcting errors, sometimes the error line may not show any issues, so it’s necessary to check if the previous line is missing a semicolon.
{ z=x+y;
t=z/100;
printf(“%f”,t);
}
For compound statements, the final semicolon in the last statement cannot be omitted (this is different from PASCAL).
6. Adding an extra semicolon.
For a compound statement, such as:
{ z=x+y;
t=z/100;
printf(“%f”,t);
};
There should not be a semicolon after the closing brace of compound statements, or it will be redundant.
For example:
if (a%3==0);
I++;
This intends to increment ‘I’ by 1 if ‘a’ is divisible by 3. However, because of the extra semicolon after if (a%3==0), the if statement ends there, and the program will execute the I++ statement regardless of whether ‘a’ is divisible by 3.
Another example:
for (I=0;I<5;I++);
{scanf(“%d”,&x);
printf(“%d”,x);}
This intends to input 5 numbers sequentially, outputting each number after input. However, due to the extra semicolon after for(), the loop body becomes an empty statement, and only one number can be input and output.
7. Forgetting to add the address operator ‘&’ when inputting variables.
int a,b;
scanf(“%d%d”,a,b);
This is invalid. The scanf function works by storing values into ‘a’ and ‘b’ at their memory addresses. ‘&a’ refers to the address of ‘a’ in memory.
8. The input method does not match the requirements.
①scanf(“%d%d”,&a,&b);
When inputting, commas cannot be used as separators between two data items, as shown below:
3,4
When inputting data, a single space or multiple spaces, or the enter key or tab key can be used as separators.
②scanf(“%d,%d”,&a,&b);
C specifies that if there are other characters in the format control string besides format specifications, the input must match those characters. The following input is valid:
3,4
In this case, using spaces or other characters instead of commas is incorrect.
3 4 3:4
Another example:
scanf(“a=%d,b=%d”,&a,&b);
The input should be in the following form:
a=3,b=4
9. The input character format does not match the requirements.
When using the “%c” format to input characters, both space characters and escape characters are treated as valid characters.
scanf(“%c%c%c”,&c1,&c2,&c3);
If the input is a b c,
the character ‘a’ is assigned to c1, the space character to c2, and the character ‘b’ to c3, because %c only requires reading one character, and spaces are not needed as separators.
10. The data types of input and output do not match the format specifiers used.
For example, ‘a’ is defined as an integer, and ‘b’ is defined as a float:
a=3;b=4.5;
printf(“%f%d\n”,a,b);
The compiler does not give an error message during compilation, but the runtime result will not match the intended outcome. This type of error requires special attention.
11. Attempting to specify precision when inputting data.
scanf(“%7.2f”,&a);
This is invalid; precision cannot be specified during data input.
12. Missing the break statement in a switch statement.
For example: printing the percentage score range based on exam grades.
switch(grade)
{ case ‘A’:printf(“85~100\n”);
case ‘B’:printf(“70~84\n”);
case ‘C’:printf(“60~69\n”);
case ‘D’:printf(“<60\n”);
default:printf(“error\n”);
Due to the missing break statement, the case acts only as a label and does not function as a condition. Therefore, when the value of ‘grade’ is ‘A’, the printf function executes the first statement and continues to execute the second, third, fourth, and fifth printf function statements. The correct approach should add “break;” after each branch. For example:
case ‘A’:printf(“85~100\n”);break;
13. Ignoring the subtle differences between while and do-while statements.
(1)main()
{int a=0,I;
scanf(“%d”,&I);
while(I<=10)
{a=a+I;
I++;
}
printf(“%d”,a);
}
(2)main()
{int a=0,I;
scanf(“%d”,&I);
do
{a=a+I;
I++;
}while(I<=10);
printf(“%d”,a);
}
It can be seen that when the input value of ‘I’ is less than or equal to 10, both yield the same result. However, when ‘I’ is greater than 10, the results differ. The while loop checks the condition before executing, while the do-while loop executes before checking. Thus, for values greater than 10, the while loop does not execute the loop body at all, while the do-while statement executes the loop body once.
14. Misusing variables when defining arrays.
int n;
scanf(“%d”,&n);
int a[n];
The expression in brackets after the array name must be a constant expression, which can include constants and symbolic constants. C does not allow dynamic definitions of array size.
15. Mistaking the defined “number of elements” for the maximum index value when defining an array.
main()
{static int a[10]={1,2,3,4,5,6,7,8,9,10};
printf(“%d”,a[10]);
}
C language specifies that defining “a[10]” means the array ‘a’ has 10 elements, with index values starting from 0, so array element ‘a[10]’ does not exist.
16. Not using static storage when initializing arrays.
int a[3]={0,1,2};
This is an incorrect way to initialize an array. C language specifies that only static arrays and external arrays can be initialized. It should be:
static int a[3]={0,1,2};
17. Adding the address operator ‘&’ in places where it should not be added.
scanf(“%s”,&str);
C language compiler treats the array name as the starting address of the array, and the input items in the scanf function do not require an address operator ‘&’. It should be:
scanf(“%s”,str);
18. Simultaneously defining parameters and local variables in a function.
int max(x,y)
int x,y,z;
{z=x>y?x:y;
return(z);
}
Parameters should be defined outside the function body, while local variables should be defined within the function body. It should be:
int max(x,y)
int x,y;
{int z;
z=x>y?x:y;
return(z);
}