C compiled programs are not as strict as other high-level languages. This gives programmers the "flexibility", but still because of this flexibility to debug the program has brought a lot of inconvenience, especially for beginners of the C language, often a few even they do not know where the mistake. Look at the wrong program, I do not know how to change, through the study of C, accumulated some C programming often make mistakes, for reference.
1. When writing identifiers, the difference between uppercase and lowercase letters is ignored.
Main ()
{
int a=5;
printf ("%d", A);
}
The compiler says that a and a are two different variable names, and the error message is displayed. C considers uppercase and lowercase letters to be two different characters. Traditionally, the symbol constant name is capitalized and the variable name is in lowercase to increase readability.
2, ignoring the type of the variable, the operation of the illegal.
Main ()
{
float a,b;
printf ("%d", a%b);
}
% is the remainder operation, and A/b is the whole residue. Integer variables A and B can be evaluated, while real variables do not allow for "redundancy" operations.
If you want to learn C/s can come to this group, first is 330, the middle is 859, the last is 766, there are a lot of learning materials can be downloaded.
3, the character constants volume and string constants confused.
Here it confuses the character constants with the string constant, which is a single character enclosed by a pair of single quotes, and a string constant is a sequence of characters enclosed by a pair of double quotes. C stipulates "\" As the end of the string, it is automatically added by the system, so the string "a" actually contains two characters: ' A ' and ', ' and it is not possible to assign it to a character variable.
4, ignoring the "=" and "= =" difference.
In many high-level languages, the "=" symbol is used as the relational operator "equals". As in basic programs, you can write
if (a=3) then ...
but in the C language, "=" is the assignment operator, and "= =" is the relational operator. Such as:
if (a==3) a=b;
the former is compared, and a is equal to 3, which means that if a and 3 are equal, the B value is assigned to a. Because of habit problems, 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, "b=2", is also part of the previous line of statements, which can cause syntax errors. When you make a mistake, sometimes you don't find an error in a row that is pointed out to be wrong, you need to see if the previous line misses a semicolon.
{
z=x+y;
t=z/100;
printf ("%f", t);
}
For a compound statement, 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);
};
The original intention is to enter 5 number, each input a number and then output it. Because a semicolon is added after the for (), the loop body becomes an empty statement, and you can only enter a number and output it.
7, input variables forget to add the address operator "&".
int a,b;
scanf ("%d%d", a,b);
This is not legal. The role of the scanf function is to store the values of A and B in the address of A and B in memory. "&a" means the address in memory of a.
8, the way to enter data and requirements do not match.
①SCANF ("%d%d", &a,&b);
When you enter, you cannot use commas as delimiters between two data, such as if the following entry is illegal:
3,4
When you enter data, you can use the ENTER key between two data at one or more space intervals, and the Key tab.
②SCANF ("%d,%d", &a,&b);
C stipulates that if there are other characters in the format control string in addition to the format description, you should enter the same characters as the characters when you enter data. The following input is legal:
3,4
It is not correct to use a space or other character without commas 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 requirements are inconsistent.
When you enter a character in the "%c" format, both the space character and the escape character are entered as valid characters.
scanf ("%c%c%c", &C1,&C2,&C3);
If you enter a b C
The character "A" is given to the C1, the character "" is given to the C2, and the character "B" is given to C3, because%c only requires a character to be read, followed by no space as a two-character interval.
10. The data type of the input and output is inconsistent with the format specifier used.
For example, a is defined as an integral type, and B is defined as a solid type
a=3;b=4.5;
printf ("%f%d\n", a,b);
No error message is given at compile time, but the result will be inconsistent with the original intention. This error is especially noteworthy.
11. When entering data, attempt to specify precision.
scanf ("%7.2f", &a);
It is illegal to do this, and you cannot specify precision when entering data.
The break statement is omitted from the 12.switch statement.
For example: Print out hundred several paragraphs based on the grade of the test.
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");
}
As the break statement is omitted, the case only acts as a label, but it does not judge the function. 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 "break;" after each branch. For example
Case ' A ':p rintf ("85~100\n");
13, ignoring the distinction between 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 first executed and then judged. The number of while loops that are greater than 10 do not execute the loop body at once, and 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, enclosed in square brackets, is a constant expression that can include constants and symbolic constants. That is, C does not allow for dynamic definition of the size of the array.
15, in defining the array, the definition of the "number of elements" mistaken for the maximum subscript value can be made.
Main ()
{
static int a[10]={1,2,3,4,5,6,7,8,9,10};
printf ("%d", a[10]);
The C language stipulates: Defined with A[10], which means that a array has 10 elements. The subscript value starts at 0, so the array element a[10] does not exist.
16. Add the address operator at the location where address operators & should not be added.
scanf ("%s", &STR);
C language Compilation system array name processing is: The array name represents the starting address of the array, and the scanf function of the entry is a character array name, no need to add the address character &. should read:
scanf ("%s", str);
17. 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 function body, 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);
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.