C language programming is easy to make a problem collection

zhaozj2021-02-12  145

Issue Date: March 18, 2004 Author: He Jun 2052 readers have already read the article

The common characteristics of common errors in C are: Strong function, convenient use. The C-compiled procedures are not as strict, so that the programmer has left "flexible room", but because this flexibility has brought a lot of inconvenience, especially for beginner C language. When people say, they often don't know where they are wrong. Looking at the wrong procedure, I don't know how to change, I have accumulated some errors that C-programmatically committed the mistakes by learning from C, and wrote to the students for reference.

1. When writing the identifier, the difference between case the case is ignored. Main () {Int a = 5; Printf ("% D", a);} Compiler The A and A are considered to be two different variable names, while displacing the error information. C believes uppercase letters and lowercase letters are two different characters. Habitually, symbol constant names, variable names are smaller, to increase readability.

2. Ignore the type of variable, which is not legal operation. Main () {float a, b; printf ("% d", a% b);}% is the surplus operation to obtain the entire number of A / B. Integer variables A and B can be submitted, while the real variables do not allow "resufficient" operations.

3. Confused character constants with string constants. CHAR C; c = "a"; confused here, character constant, character constant, a single character, a string constant, is a pair of two quotes enclosed. Sequence. C specifying the "/" string end flag, which is automatically added by the system, so the string "a" actually contains two characters: 'a' and '/', which gives it a character variable Yes no.

4. Ignore the difference between "=" and "==". In many advanced languages, use "=" symbol as the relational operator "equal". If you can write if (a = 3) Then ... but C language, "=" is an assignment operator, and "==" is a relational operator. Such as: if (a == 3) a = b; the former is compared, whether a is equal to 3, and the latter indicates that the B value is assigned to A if A and 3 are equal. Because of habit, beginners tend to make such mistakes.

5. Forget the semicolon. The semicolon is an indispensable part in the C statement. There must be a semicolon at the end of the statement. When a = 1b = 2 compiles, the compiler does not find the semicolon after "a = 1", and the next line "B = 2" is also part of the previous line statement, which will have a syntax error. When it is wrong, sometimes there is no mistake in the wrong line that is pointed out, it is necessary to see if the last line will miss the semicolon. {z = x y; t = z / 100; Printf ("% f", t);} For the composite statement, the last semicolon in the last statement cannot be ignored (this is different from PASCAL) .

6. Add a number of semicolons. For a compound statement, such as: {z = x y; t = z / 100; printf ("% f", t);}; after the composite statement, the semicolon should not be added, otherwise the snake will be added. Another example is: if (a% 3 == 0); i ; this is if 3 is except A, then i plus 1. However, since the semicolon is added more than if (a% 3 == 0), the IF statement is over, the program will execute the I statement, regardless of whether the entire A, i will be automatically added 1. Another example: for (i = 0; i <5; i ); {scanf ("% d", & x); Printf ("% d", x);} It is intended to enter 5 numbers, each input Then output it again. Since the FOR () adds a semicolon to make the cyclic body becomes a null statement, only one number can be entered and output it. 7. Forget the address operator "&" when entering the variable. INT A, B; Scanf ("% D% D", A, B); this is not legal. The scanf function is to store the value of A and B in accordance with A and B's address in the memory. "& A" refers to the address in memory.

8. The way to enter data does not match the requirements. 1SCANF ("% D% D", & A, & B); When entering, the comma cannot be used as a separator between the two data, as in the following input is not legal: 3, 4 input data, between the two data Or multiple space intervals, you can also use the Enter key to jump Tab. 2SCANF ("% D,% D", & A, & B); C. C: If there are other characters in the "Format Control" string, there are other characters in addition to the format description, the same characters as these characters should be input when entering the data. The following input is legal: 3, 4 This time does not have a comma and use spaces or other characters. 3 4 3: 4 Another example: scanf ("A =% D, B =% D", & A, & B); input should be as follows: a = 3, b = 4

9. The format of the input character is inconsistent with the requirements. When entering characters in "% C" format, "space characters" and "escape characters" are input as a valid character. Scanf ("% C% C% C", & C1, & C2, & C3); if the input ABC character "A" gives C1, characters "" gives C2, character "b" is given to C3 because% c is only necessary to read Enter a character, there is no need to use spaces as the interval of two characters.

10. The data type of the input and output is inconsistent with the format specifier used. For example, A is defined as integer, B is defined as real A = 3; b = 4.5; Printf ("% f% D / N", A, b); compile time does not give error information, but the results of operation Not in conformity with origin. This mistakes are particularly important.

11. When entering data, attempt specifying accuracy. Scanf ("% 7.2f", & a); this is not legal, and the accuracy cannot be specified when entering data.

12. Switch statement leaks the BREAK statement. For example, the percentage segment is printed according to the level of the test score. Switch {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 leaking Break statement, Case only starts the labeling, and cannot determine. Therefore, when the grade value is A, the PrintF function will then perform the second, third, fourth, and five PRINTF function statements after executing the first statement. Correct writing should add "BREAK;" after each branch. For example, Case 'A': Printf ("85 ~ 100 / N"); Break; 13. Ignore the difference between the While and Do-While statements in detail. (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 value of the input I is less than or equal to 10, the results obtained are the same. And when I> 10, the two results are different. Because the While loop is executed first, the Do-While loop is executed first. For the number of While cycles greater than 10, the cyclic body is not executed once, while the Do-While statement is executed a cyclic body.

14. Define misuse variables when arrays. INT N; Scanf ("% D", & n); int A [n]; Number of group names enclosed in square brackets, can include constant and symbol constants. That is, C does not allow the size of the array to dynamically define.

15. When defining an array, the defined "element" is mistakenly considered to be the maximum subscript value. Main () {static int A [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Printf ("% D", A [10]);} C language regulations : Use a [10] when defined, indicating that 10 elements of A array. The following standard value begins by 0, so the array element a [10] does not exist.

16. When the initialization array is used, it is not used for static storage. INT A [3] = {0, 1, 2}; this initialization array is wrong. The C language specifies that only static storage (STITIC) arrays and external storage (Exterm) arrays can initialize. Should be changed to: static int a [3] = {0, 1, 2};

17. Address operators in the location where address operators should not be addressed. Scanf ("% s", & str); C language compilation system to the number of group names is: the array name represents the start address of the array, and the input item in the scanf function is the character number group name, and it is not necessary to add addresses & . Should be changed to: scanf ("% s", str);

18. Simultaneously define local variables in the shape and function. INT MAX (X, Y) INT X, Y, Z; {z = x> y? x: y; Return (z);} Ground should be defined in vitro, and local variables should be defined in the function. It should be changed to: int MAX (x, y) int x, y; {int z; z = x> y? X: y; return (z);}

转载请注明原文地址:https://www.9cbs.com/read-7530.html

New Post(0)