How to find program errors quickly and efficiently (C-language)

Source: Internet
Author: User

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)

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.