C language common error summary

xiaoxiao2021-03-06  76

C language FAQ Summary: Tianjin Xiaoge 2002 January 12, 14:07 The biggest feature of C language is: 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 message. 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, you cannot use a comma to make two data between two data, such as the following input: 3, 4  When entering the data, between the two data One or more space intervals, or use Enter keys, jumping 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 , such as: scanf ("A =% D, B =% D", & A, & B); input should be as follows: a = 3, b = 49. Enter the format of the character The requirements are inconsistent. 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 ABC character "A" gives C1, the character "" is given to C2, the character "b" is given to C3, because% c only required Read in 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.

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

New Post(0)