Home > Article > Backend Development > Debugging method of c language program
Debugging method of C language program
The so-called program debugging refers to the error checking and troubleshooting of the program.
Debugging programs should generally go through the following steps:
1. First, perform manual inspection, that is, static inspection.
After writing a program, do not rush to the computer, but perform manual inspection of the program. This step is very important because it can detect most of the errors caused by the programmer's negligence. This step is often overlooked by people, who always want to push everything to the computer, but this will take up more machine time. As a programmer, you should develop a rigorous style, strictly control every step, and do not leave problems to The subsequent process. (Recommended learning:
c language video tutorial) In order to carry out manual inspection more effectively, the compiled program should strive to achieve the following points:
① Programming should be done using a structured programming method to increase readability;2. After the manual inspection is correct, go on the computer for debugging.② Add as many comments as possible to help understand the role of each program;
③ Do not write complex programs Write all statements in the main function, and use more functions to implement a separate function. In addition to using
parameters to transfer data, there should be as few coupling relationships between functions as possible, so that they can be easily checked and processed separately.
Discovering errors through computer testing is called dynamic inspection. Syntax error information will be given during compilation. During debugging, you can use the prompt information to find out the errors in the program and correct them. It should be
It should be noted that sometimes the place where the error is prompted is not the actual error location. If you cannot find the error in the line where the error is prompted, you should go to the previous line and try again. Sometimes the type of error message is not absolutely accurate. Since there are many error situations and various errors are related to each other, you must be good at analyzing and find the real errors, rather than just looking for error messages in a literal sense.
If the system prompts a lot of error messages, you should correct them one by one from top to bottom. Sometimes a large piece of error information is displayed, which often makes people feel that the problem is serious and they have no way to start. In fact, there may be only one or two errors. For example, if a variable used is not defined, an error message will be issued to all statements containing the variable during compilation. At this time, as long as a variable definition is added, all errors will be eliminated.
3. After correcting the syntax errors (including "error" and "warning"), the program will obtain the executable target programthrough link. Run the
program and enter the data required by the program to get the results. The running results should be analyzed to see if they meet the requirements. Some beginners think that there is no problem when they see the running results without careful analysis, which is dangerous. Sometimes, the data is complex and it is difficult to immediately judge whether the results are correct. You can consider a batch of "test data" in advance. Entering these data can easily determine whether the results are correct or not. For example, when solving the equation ax2 bx c=0 and inputting the values of a, b, and c as 1, -2, and 1 respectively, the value of the root x is 1. This is easy to judge. If the root is not equal to 1, the program is obviously wrong.
However, when using "test data", the program running results are correct, and there is no guarantee that the program is completely correct. Because it is possible that the results will be incorrect when another set of data is entered. For example, if you use a formula to find the value of the root a=0 has not been defended and pluralized). Therefore, you should try every situation that the program may encounter one by one. For example, if the if statement has two branches, it is possible that the result of the program is correct when passing through one of the branches, but the result is incorrect when passing through the other branch. Everything must be considered. In fact, when the program is complex, it is difficult to test all possible situations. Just select typical critical data for testing.
4. The running results are incorrect, mostly logical errors. Such errors often require careful inspection and analysis to be discovered. The following methods can be used: 1. Carefully compare the program with the flow chart. If the flow chart is correct, it is easy to find if the program is wrong. For example, if you forget to write curly braces in a compound statement, you can quickly find it by looking at the flow diagram.2. If you really can't find the error, you can use the "segment inspection" method. Set up several printf function statements at different locations in the program to output the values of relevant variables and check them section by section. Until it is found that the data in a certain section is incorrect. At this time, the error has been confined to this paragraph. By continuously reducing the "error checking area", you can find the error.
3. You can also use the "conditional compilation" command to debug the program (during the program debugging phase, several printf function statements will be compiled and executed. When debugging is completed, these statementsNo longer needs to be compiled, and it will no longer be executed). This method eliminates the need to remove printf function statements one by one to improve efficiency.
4. If no problem is found in the program, check the flow chart for errors, that is, whether there are any problems with the algorithm. If so, correct it and then modify the program.
5. Some systems also provide debug tools to track programs and give corresponding information, which is more convenient to use. Please consult the relevant manuals.
In short, program debugging is a detailed and in-depth work that requires hard work, brain use, and accumulation of experience. The process of program debugging often reflects a person's level, experience
, and attitude. I hope everyone will pay enough attention to it. The purpose of debugging a program on a computer is never to "verify the correctness of the program", but to "master the methods and techniques of debugging". You must learn to find problems by yourself, so that you will slowly write practical programs with fewer errors.
For more programming tutorials about c language, please pay attention to the PHP Chinese website.
The above is the detailed content of Debugging method of c language program. For more information, please follow other related articles on the PHP Chinese website!