Analysis of Common Errors in C Language Learning

Source: Internet
Author: User
Tags float max

Analysis of Common Errors in C Language Learning

Error category
Syntax Error

Logical error

Running Error
0. Forget to define the variable:
Int main ()
{

X = 3; y = 6;
Printf ("% d/n", x + y );

}

1. C language variables must be defined before they can be used;

2. the type of input and output data is different from the format specifier used
Int a = 3; float B = 4.5; printf ("% f % d/n", a, B );

They are not converted according to the assignment rules (for example, 4.5 is converted to 4 ), instead, the data format in the storage unit is organized and output according to the format characters (for example, B occupies 4 bytes, and only the data of the last two bytes is output by % d as an integer)

3. Do not pay attention to the value range of int type data
The value range of int type data (-32768 ~ 32768)
Int num = 89101;
Printf ("% d", num );
It will take over 16 digits to get 23563.
Note: The long type is defined, and the "% d" operator is still used in the output. The above error will still occur.

4. You forgot to use the address character when entering the variable.
Scanf ("% d", a, B );

5. The organization and requirements for data input are inconsistent
Except the format specifiers, the format strings in the scanf function must be input as is.

6. mistaken "=" as "equal to" comparison character
"=" Is a value-added operator
"=" Is a comparison operator.


7. The semicolon is missing after the statement
{
T =;
A = B;
B = t
}
It is pascal's syntax.

8. Add a semicolon to the place where the number should not be added
If (a> B );
Printf ("a is larger than B/n ");

For (I = 0; I <10; I ++ );
{
Scanf ("% d", & x );
Printf ("% d/n", x * x );
}

9. For the compound statement that should contain curly arc, forget to add the curly arc.
Sum = 0;
I = 1;
While (I <= 100)
Sum = sum + 1;
I ++;
10. The arc is not paired.
While (c = getchar ()! = '#')
Putchar (c );

11. I forgot the differences between uppercase and lowercase letters when using the logo.
{
Int a, B, c;
A = 2;
B = 3;
C = A + B;
Printf ("% d + % d = % D", A, B, C );
}


12. Misuse of Circular Arc When referencing array elements
{
Int I, a (10 );
For (I = 0; I <10; I ++)
Scanf ("% d", & a (I ));
}

13. When defining an array, the defined "number of elements" is mistaken for the "Maximum number of available sub-values"
{
Int a [10] = {1, 2, 4, 5, 6, 7, 8, 9, 10 };
Int I;
For (I = 1; I <= 10; I ++)
Printf ("% d", a [I]);
}

14. The definition and reference methods for two-dimensional or multi-dimensional arrays are incorrect.
{
Int a [5, 4];
...
Printf ("% d", a [1 + 2, 2 + 2]);
...
}

15. The array name indicates all elements in the array.
{Int a [4] = {1, 2, 3, 4 };
Printf ("% d", );
}


16. Differences between character arrays and character pointers
Main ()
{
Char str [40];
Str = "Computer and c ";
Printf ("% s/n", str );
}


17. No definite value is assigned to the pointer variable before it is referenced.
{
Char * p;
Scanf ("% s", p );
}

{
Char * p, c [20];
P = c;
Scanf ("% s", p );
}

18. The break statement is missing from the branches of the switch statement.
Mixed character and string representation
...
Char sex;
Sex = "M ";
...

19. errors caused by the use of the auto-increment (++) and auto-Subtract (--) Operators
{
Int * p, a [5] = {1, 3, 5, 7, 9 };
P =;
Printf ("% d", * p ++ );
}

Note the difference between * (++ p;

20. The called function is defined after the call statement, but not described before the call.
Main ()
{Float x, y, z;
X = 3.5; y =-7.6;
Z = max (x, y );
Printf ("% f", z );
}
Float max (float x, float y)
{
Return (x> y? X: y );
}

21. Mistaken changes to the parameter value will affect the value of the real parameter.
Swap (int x, int y)
{Int t;
T = x; x = y; y = t;
}
Main ()
{Int a, B;
A = 3; B = 4;
Swap (a, B );
Printf ("% d, % d/n", a, B );
}

22. The real parameters and parameter types of the function are inconsistent.
Fun (float x, float y)
Main ()
{
Int a = 3, B = 4;
C = fun (a, B );
...
}

23. Mixed Type pointers of different classes
{
Int I = 3, * p1;
Float a = 1.5, * p2;
P1 = & I; p2 = &;
P2 = p1;
Printf ("% d, % d/n", * p1, * p2 );
}

24. Do not pay attention to the order in which function parameters are evaluated
Int I = 3;
Prinft ("% d, % d, % d/n", I, ++ I, ++ I );
Result 5, 4
Because VC ++ calculates the function value in the order from right to left.
The C standard does not specify the order in which function parameters are evaluated.

25. Differences between obfuscation array names and pointer Variables
{Int I, a [5];
For (I = 0; I <5; I ++)
Scanf ("% d", a ++ );
}

{Int a [5], * p;
P =;
For (int I = 0; I <5; I ++)
Scanf ("% d", p ++)
}
{Int a [5], * p;
For (p = a; p scanf ("% d", p );
}

26. Differences between obfuscation struct types and struct Variables
Struct worker
{Long int num;
Char name [20];
Char sex;
Int age;
};
Worker. num= 187045;
Strcpy (worker. name, "ZhangFun ");
Worker. sex = 'M ';
Worker. age = 18;

27. I forgot to open the file while using it. I opened it in read-only mode, but tried to output data to the file.
If (fp = fopen ("test", "r") = NULL)
{Printf ("cannot open this file/n ");
Exit (0 );
}
Ch = fgetc (fp );
While (ch! = '#')
{Ch = ch + 4;
Fputc (ch, fp );
Ch = fgetc (fp );
}

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.