Zookeeper
C-compiled programs are not as strict as other advanced languages, leaving the programmer with "flexibility ", however, due to this flexibility, debugging of the program is inconvenient. Especially for beginners of C language, errors that do not even know where the errors are. Looking at the wrong program, I do not know how to change it. Through studying C, I have accumulated some errors that C programming often makes for reference.
1. When writing an identifier, the differences between uppercase and lowercase letters are ignored.
Main ()
{
Int a = 5;
Printf ("% d", );
}
The compiler considers a and A as two different variable names, and displays error information. C thinks that uppercase and lowercase letters are two different characters. Traditionally, the symbolic constant name is in upper case, and the variable name is in lower case to increase readability.
2. The variable type is ignored and illegal operations are performed.
Main ()
{
Float a, B;
Printf ("% d", a % B );
}
% Is the remainder operation to obtain the integer remainder of a/B. Integer Variables a and B can perform the remainder operation, while real variables do not allow the remainder operation.
3. Confuse character constants with string constants.
Char c;
C = "";
Character constants and string constants are obfuscated here. character constants are a single character enclosed by a pair of single quotes, and string constants are character sequences enclosed by a pair of double quotes. C indicates that "\" is used as the string end sign. It is automatically added by the system. Therefore, the string "a" actually contains two characters: 'A' and '\ 0 ', however, it cannot be assigned to a character variable.
4. the difference between "=" and "=" is ignored.
In many advanced languages, the "=" symbol is used as the relational operator "equal ". For example, you can write
If (a = 3) then...
In C, "=" is the value assignment operator, and "=" is the relational operator. For example:
If (a = 3) a = B;
The former is a comparison, and whether a is equal to 3. The latter indicates that if a and 3 are equal, the B value is assigned to. Due to habits, beginners often make such mistakes.
5. Forget the plus points.
A semicolon is an indispensable part of a C statement. It must end with a semicolon.
A = 1
B = 2
During compilation, the Compilation Program does not find a semicolon after "a = 1", so the next line "B = 2" is also part of the previous line of statements, which leads to a syntax error. When an error is corrected, if no error is found in the wrong line, check whether the semicolon is missing in the previous line.
{
Z = x + y;
T = z/100;
Printf ("% f", t );
}
For compound statements, the last semicolon in the last statement cannot be ignored or not written (this is different from PASCAL ).
6. Add a semicolon.
For a composite statement, such:
{
Z = x + y;
T = z/100;
Printf ("% f", t );
};
You should not add a semicolon after the curly braces of the compound statement. Otherwise, it will be superfluous. Another example:
If (a % 3 = 0 );
I ++;
If 3 is divisible by a, I adds 1. However, because if (a % 3 = 0) is added with a semicolon, the if statement ends here and the program will execute the I ++ statement, regardless of whether 3 is divisible by, I will automatically add 1. Another example is:
For (I = 0; I <5; I ++ );
{
Scanf ("% d", & x );
Printf ("% d", x );
}
The intention is to input 5 numbers each time and then output them. Because a semicolon is added after for (), the loop body becomes an empty statement. At this time, only one number can be input and output.
7. When entering a variable, the address operator "&" is missing.
Int a, B;
Scanf ("% d", a, B );
This is invalid. The Scanf function is used to store the values of a and B in the memory according to the address of a and B. "& A" refers to the address of a in memory.
8. The input data method is inconsistent with the requirements.
① Scanf ("% d", & a, & B );
When entering data, you cannot use commas (,) as the delimiter between the two data. The following input is invalid:
3, 4
When you enter data, one or more spaces are separated between the two data. You can also use the Enter key and hop key tab.
② Scanf ("% d, % d", & a, & B );
C: If there are other characters besides the format description in the "format control" string, you should enter the same characters as these characters when entering data. The input below is valid:
3, 4
It is incorrect to use spaces or other characters instead of commas.
3 4: 4
Another example:
Scanf ("a = % d, B = % d", & a, & B );
The input format should be as follows:
A = 3, B = 4
9. The format and requirements of the input characters are different.
When the characters are entered in "% c" format, "space character" and "Escape Character" are both valid characters.
Scanf ("% c", & c1, & c2, & c3 );
For example, enter a B c
The character "a" is sent to c1, the character "is sent to c2, and the character" B "is sent to c3, because % c requires only one character to be read, and spaces are not used as the interval between the two characters.
10. The input and output data types are inconsistent with the format specifiers used.
For example, a is defined as an integer, and B is defined as a real type.
A = 3, B = 4.5;
Printf ("% f % d \ n", a, B );
No error information is provided during compilation, but the running result is inconsistent with the original intention. Pay special attention to this error.
11. When inputting data, an attempt is made to specify the precision.
Scanf ("% 7.2f", & );
This is not legal. Precision cannot be specified when you input data.
12. The break statement is missing in the switch statement.
For example, the percentage system is printed Based on the grade of the test score.
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 ");
}
Because the break statement is missing, case only plays a label role, but does not have a judgment function. Therefore, when the grade value is A, the printf function then runs the second, third, fourth, and fifth printf function statements after the first statement is executed. The correct method should be followed by "break;" after each branch ;". For example
Case 'A': printf ("85 ~ 100 \ n "); break;
13. The difference between the while and do-while statements in details is ignored.
(1) main ()
{
Int a = 0, I;
Scanf ("% d", & I );
While (I <= 10)
{A = a + I;
I ++;
}
Printf ("% d", );
}
(2)
Main ()
{
Int a = 0, I;
Scanf ("% d", & I );
Do
{A = a + I;
I ++;
} While (I <= 10 );
Printf ("% d", );
}
We can see that when the input I value is less than or equal to 10, the results are the same. When I> 10, the two results are different. Because the while loop is first judged and then executed, while the do-while loop is first executed and then judged. If the number is greater than 10, the loop body is not executed once while the do-while statement is executed once.
14. Misuse of variables when defining arrays.
Int n;
Scanf ("% d", & n );
Int a [n];
After the array name is enclosed in square brackets, it is a constant expression, which can include constants and symbolic constants. That is, C does not allow dynamic definition of the array size.
15. When defining an array, the defined "number of elements" is mistakenly regarded as the maximum value that can be used.
Main ()
{
Static int a [10] = {1, 2, 4, 5, 6, 7, 8, 9, 10 };
Printf ("% d", a [10]);
}
C language: Use a [10] to define a array with 10 elements. The value of the lower Mark starts from 0, so the array element a [10] does not exist.
17. The address operator should not be added to the position where the address operator & is not added.
Scanf ("% s", & str );
The C language compilation system processes the array name: the array name represents the starting address of the array, and the input item in the scanf function is the character array name. You do not need to add the address character &. Should be changed:
Scanf ("% s", str );
18. Local variables in the parameters and functions are defined at the same time.
Int max (x, y)
Int x, y, z;
{
Z = x> y? X: y;
Return (z );
}
Parameters should be defined in the function body, while local variables should be defined in the function body. Should be changed:
Int max (x, y)
Int x, y;
{
Int z;
Z = x> y? X: y;
Return (z );
}