I think most of the people who have just started to learn C are the same as I used to, their own hard to knock out the program code in the debugging run error, but I do not know where the error, how to modify. Most people's first reaction is to feel the mind, and then simply according to their own logic to modify the code, the result is more wrong. Here are some of the methods I've summarized to find errors.
First find out if the code has a logic error:
1. The variable is not used before the value
Instance:
int main ()
{
int x=5,y,z;
Z=x*y;
printf ("%d", z);
return 0;
}
In this program, the variable y is not assigned before it is used, so the value of the output z will be the system randomly assigns a value to Y multiplied by the value of X.
2. Confuse assignment assignment symbol "=" with equals sign "= ="
Instance:
There are statements:
if (a=5)
{
printf ("A equals 5");
}
The result of this output is always "a equals 5", because at the time of execution of the IF (a=5), no matter what the value before a is, it will be re-assigned to 5, instead of comparing the value of a with 5, and the correct representation of the comparison is:
if (a==5)
{
printf ("A equals 5");
}
Missing in the Break,do{}while () statement in the 3.switch statement () after the ";" Wait a minute.
Next, find out if the code has an algorithmic error:
An example of a worthy algorithm for exchanging two variables:
The correct algorithm is as follows:
int main ()
{
int a=5,b=10,temp;
Temp=a;
A=b;
B=temp;
printf ("%d%d", A, b);
return 0;
}
Swapping the values of A and B is not achieved if the order of temp=a;a=b;b=temp is reversed.
The last and easiest mistake to make:
For example, after the end of a statement forgot to hit ";", the input statement "scanf ("%d ", a)" in the assignment parameter a before the address character "&" and so on.
I would like to follow the above steps to find the code error will become fast and effective, may wish to try Oh!
How to find program errors quickly and efficiently (C-language)