The most important features of C language are: Strong function, easy to use and flexible. C Compiled program for grammar check is not as strict as other high-level language, this gives programmers leave "flexible room", but still because of this flexibility to the debugging of the program brought a lot of inconvenience, especially for beginners of C language, often out of some even they do not know where the wrong mistake. Looking at the wrong procedure, I do not know how to change, I through the study of C, accumulated a number of C programming mistakes often made, write to the Students for reference
1. When writing identifiers, the difference between uppercase and lowercase letters is ignored.
Main ()
{
int a=5;
printf ("%d", A);
}
The compiler considers a and a to be two different variable names, and displays an error message. C considers uppercase and lowercase letters to be two different characters. In practice, the symbolic constant name is capitalized, and the variable name is represented in lowercase to increase readability.
2. The type of the variable is ignored and an illegal operation is performed.
Main ()
{
float A, B;
printf ("%d", a%b);
}
% is the remainder operation, and A/b is obtained. Integer variables A and B can be used for redundancy operations, while real variables do not allow "redundancy" operations.
3. Confuse character constants with string constants.
char c;
C= "a";
Here, a character constant is confused with a string constant, which is a single character enclosed by a pair of single quotes, and the string constant is a sequence of characters enclosed in double quotation marks. c Specifies that the "" as a string end flag, which is automatically added by the system, so the string "a" actually contains two characters: ' A ' and ', It is not possible to assign it to a character variable.
4. The difference between "=" and "= =" is ignored.
In many high-level languages, the "=" symbol is used as the relational operator "equals". As can be written in the basic program
if (a=3) then ...
In the C language, "=" is an assignment operator, and "= =" is a relational operator.
if (a==3) a=b;
The former is for comparison, whether a is equal to 3, the latter indicates that if a and 3 are equal, the B value is assigned to a. Beginners tend to make such mistakes because of habit problems.
5. Forget to add a semicolon.
Semicolons are an integral part of the C statement and must have a semicolon at the end of the statement.
A=1
b=2
At compile time, the compiler does not find a semicolon after "a=1", and the next line of "b=2" is also part of the previous line of statements, which causes a syntax error. Sometimes, in a row that is indicated to be wrong, you need to see if the previous line has missed the semicolon.
{
Z=x+y;
t=z/100;
printf ("%f", t);
}
For a compound statement, the last semicolon in the last statement cannot ignore the non-write
(This is different from Pascal).
6. Add a semicolon.
For a compound statement, such as:
{
Z=x+y;
t=z/100;
printf ("%f", t);
};
You should not add a semicolon after the curly braces of a compound statement, otherwise it will be superfluous.
Another example:
if (a%3==0);
i++;
This is if 3 is divisible by a, then I plus 1. But since if is added with a semicolon, the IF statement ends, the program executes the i++ statement, regardless of whether 3 is divisible a,i will automatically add 1 to the a%3==0.
Again such as:
for (i=0;i<5;i++);
{scanf ("%d", &x);
printf ("%d", x);}
The intention is to enter 5 numbers successively, and then output each number after entering it. Because a semicolon is added to the for () to make the loop body an empty statement, you can enter only one number and output it.
Another example:
Forget to add the address operator "&" when entering a variable.
int A, B;
scanf ("%d%d", b);
This is not legal. The purpose of the SCANF function is to store the value of a A, a, b at the address of the memory. &a "refers to a in-memory address.
7. Input data in a manner inconsistent with the requirements.
①SCANF ("%d%d", &a,&b);
When entering, you cannot use a comma as a delimiter between two data, as the following entry is not valid:
3,4
Enter data, between two data with one or more spaces between the space, you can also use the return, the Key tab.
②SCANF ("%d,%d", &a,&b);
8.C specifies that if there are other characters in the format control string other than the format description, the characters with the same characters should be entered when the data is entered. The following input is legal:
3,4
It is not right to use a space or other character without a comma at this time.
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 format of the input character is inconsistent with the requirements.
When you enter characters in the format "%c", both the "space character" and "escape character" are entered as valid characters.
scanf ("%c%c%c", &C1,&C2,&C3);
such as input a B C
The character "A" is given to C1, the character "" is given to C2, and the character "B" is given to C3, because%c requires only one character to be read, followed by no space as a two-character interval.
10. The data type of the input and output is inconsistent with the format specifier used.
For example, a is defined as an integral type and B is defined as a real type
a=3;b=4.5;
printf ("%f%d", A, b);
No error message is given at compile time, but the result will be inconsistent with the original intent. This error is especially important.
11. Attempt to specify accuracy when entering data.
scanf ("%7.2f", &a);
It is illegal to do this, and you cannot specify the accuracy when entering data.
The break statement is omitted from the 12.switch statement.
For example: The number of percentile is printed according to the grade of exam results.
Switch (grade)
{case ' A ':p rintf ("85~100");
Case ' B ':p rintf ("70~84");
Case ' C ':p rintf ("60~69");
Case ' D ':p rintf ("<60");
default:printf ("error");
Because of the omission of the break statement, case only plays the role of marking, but does not play a role in judgment. Therefore, when the grade value is a, the printf function executes the second, three, four, and five printf function statements after the first statement is executed. The correct wording should be followed by a "break" after each branch.
For example
Case ' A ':p rintf ("85~100");
13. Ignores the difference in detail between the while and the 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);
}
As you can see, when the value of input i is less than or equal to 10 o'clock, the result is the same. And when i>10, the results are different. Because while loops are evaluated first and then executed, the Do-while loop is evaluated first. For a number greater than 10, the loop body is not executed once , and the Do-while statement executes the loop body once.
14. Misuse of variables when defining arrays.
int n;
scanf ("%d", &n);
int a[n];
The array name is enclosed in square brackets with a constant expression, which can include constants and symbolic constants. That is, C does not allow dynamic definition of the size of the array.
15. When defining an array, the "number of elements" that are defined is mistaken for the maximum possible bottom
The label value.
Main ()
{static int a[10]={1,2,3,4,5,6,7,8,9,10};
printf ("%d", a[10]);
}
C language: Defined with A[10], indicating that a array has 10 elements. The subscript value starts at 0, so the array element a[10] does not exist.
16. When initializing an array, no static storage is used.
int a[3]={0,1,2};
It is not right to initialize the array. C language provisions only static storage arrays and external storage
(exterm) array to initialize. Should be changed to: static int a[3]={0,1,2};
17. Add the address operator to the location where the address operator & is not to be added.
scanf ("%s", &STR);
C language compiler system for the processing of the array name is: The array name represents the starting address of the arrays, and the scanf function input is a character array name, no need to add the address character & should read: scanf ("%s", str);
18. The local variables in the formal parameters and functions are defined at the same time.
int Max (x, y)
int x, y, Z;
{z=x>y?x:y;
return (z);
}
Formal parameters should be defined outside the body of the function, and local variables should be defined within the function body. should read:
int Max (x, y)
int x, y;
{int z;
z=x>y?x:y;
return (z);
}
Learn C language programming beginner, easy to often out of error