C language programming often makes 18 mistakes

Source: Internet
Author: User

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, written 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. Traditionally, symbolic constant names are capitalized, and variable names are denoted in lowercase to increase readability.

2, the type of the variable is ignored, the operation is illegal.
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, the character constants and string constants are confused, character constants are single characters enclosed by a pair of single quotes, and string constants are sequences of characters enclosed in double quotation marks. c Specifies "\" as a string end flag, which is automatically added by the system, so the string "a" actually contains two characters: ' A ' and ' s ', and assigning it to a character variable is not possible.

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 ...
But in C, "=" is an assignment operator, and "= =" is a relational operator. Such as:
if (a==3) a=b;
The former is for comparison, whether a is equal to 3, and the latter indicates that if a and 3 are equal, the B value is assigned to a. Because of the habit problem, beginners often make such mistakes.

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 can cause syntax errors. Sometimes, if you don't see an error in a row that you've identified as wrong, you need to see if the previous line missed the semicolon.
{
Z=x+y;
t=z/100;
printf ("%f", t);
}
For compound statements, the last semicolon in the last statement cannot be ignored (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. However, because if (a%3==0) after the addition of semicolons, the IF statement to the end, the program will execute the i++ statement, regardless of whether the 3 divisible a,i will automatically add 1. 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.

7. Forget add address Operator "&" when entering variable.
int A, B;
scanf ("%d%d", b);
This is not legal. The function of scanf is to store the value of a and B in the memory address according to A and B. "&a" refers to the address in memory of a.

8. 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);
c Specifies that if there are other characters in the format control string other than the format description, the characters that are the same as those 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 input character format and the requirements are inconsistent.
When you enter characters in the format "%c", both the "space character" and "escape character" are entered as valid characters.
scanf ("%c%c%c", &AMP;C1,&AMP;C2,&AMP;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 input and output data types are 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\n", A, b);
No error message is given at compile time, but the result will not match the original intention. This error is especially important.

11, the input data, the attempt to specify the accuracy.
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\n");
Case ' B ':p rintf ("70~84\n");
Case ' C ':p rintf ("60~69\n");
Case ' D ':p rintf ("<60\n");
default:printf ("error\n");
}
Because of the omission of the break statement, case only plays the role of marking, and does not play a role in judgment. Therefore, when the grade value is a, the printf function executes the 第二、三、四、五个 printf function statement 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\n");

13. Ignoring the differences 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);
}
As you can see, when the value of input i is less than or equal to 10 o'clock, the results are the same. And when i>10, the results are different. Because the while loop is first judged and then executed, and the Do-while loop is executed before judgment. The loop body is not executed once for a number while loop greater than 10, while 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 defined is mistaken for the maximum subscript value that can be made.
Main ()
{static int a[10]={1,2,3,4,5,6,7,8,9,10};
printf ("%d", a[10]);
}
C language provisions: defined with A[10], indicating that a array has 10 elements. The subscript value starts with 0, so the array element a[10] does not exist.
 
17. Add an address operator to the location where the address operator & should not be added.
scanf ("%s", &AMP;STR);
The C language compilation system handles the array name by: The array name represents the starting address of the arrays, and the entry in the SCANF function is a character array name, and the address & is unnecessary. 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);
}

C language programming often makes 18 mistakes

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.