C Common Errors

Source: Internet
Author: User
The biggest feature of C language is: strong functionality, easy to use and flexible. 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 are often made in C Programming and wrote them 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 the string ends with '/0'. 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 mistaken for the maximum number of possible sub-values.

Main ()
{Static int A [10] = {1, 2, 3, 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.

16. No static storage is used during array initialization.

Int A [3] = {0, 1, 2 };
In this way, array Initialization is incorrect. C language requires that only static and exterm Arrays can be initialized. Should be changed:
Static int A [3] = {0, 1, 2 };

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. The local variables in the parameters and functions are also defined.

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 );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.