Simple single-step debugging in C language

Single-step tracing debugging in C language

Single-step debugging refers to a debugging method usually used in program development to find bugs in the program. It tracks the program execution process step by step and finds the cause of the error based on the values ​​of variables.

Let's take a simple small program as an example. The program here must be under the project. A single file cannot be debugged and will be compiled and executed directly.
The program uses the function max() to find the larger of the two variables. For simplicity, the values ​​of a and b are given here.

#include <stdio.h>
int max(int x,int y) {
    
    
	int temp;
	temp = x>y?x:y;
	/*判断形式参数x和y的大小,并赋值给temp,随后带出函数体*/
	return temp;
}
int main() {
    
    
	int a,b,result;
	a=3;
	b=4;
	result = max(a,b);
	/* 将记录 a 原始值的变量 temp,赋值给 b,实现 a 向 b 转换 */
	printf("%d\n",result);
	return 0;
}
1. Here in the main() function of the program, hit a breakpoint at line number 11 with the single-machine mouse for single-step debugging.

Insert image description here

2. Click the button with a bug below, select watches inside, and enter the changes in variable values ​​that need to be checked. As for why it is called a bug here, you can also check out the story.

Insert image description here
Select the watches inside, where you can manually enter variable names for display. I entered a, b, temp and result here. Let's take a look at what happened during the debugging process.
Insert image description here
Insert image description here

3. Click the red triangle here to start debugging (a black frame will pop up here, don’t worry about it)

Insert image description here
At this time, pay attention to observe that all the variables defined previously have some values ​​in the watches small window. These values ​​​​are all old values ​​​​before and have not been updated in this program. At the same time, there is a small yellow mark in the program to show where the current program has been executed.
Insert image description here
Insert image description here

4. Here we first introduce these commonly used function keys: ① is used to start debugging; ② is used to execute line by line, that is, the small yellow mark will run to the next line; ③ is used to enter the function body, if Direct execution line by line will not enter other functions; ④ and ③ are opposite, exit from the function body and return to the main function to continue execution. If you are interested in other buttons, you can check the information. I won’t go into them here. The one next to ① will be used in multi-line debugging.

Insert image description here

5. Next, click the No. 2 button to debug line by line. Pay attention to the changes in variable values. When the execution reaches result = max(a,b);, you can see that the values ​​​​of a and b have been displayed.

Insert image description here

6. Next, click the No. 3 button to enter the max function. With the single-step debugging in the max function, you can finally find that temp finally has the maximum value: 4, and successfully brings out the function body and sends it to main. Functional result variable.

Insert image description here

7. Finally execute the print statement, and you can see the output display 4 in the black box.

Generally, no error is reported when the program is compiled, but unexpected problems occur during execution. At this time, you can use the debugger to find out where the problem occurs in the program. Being able to debug a program and find the error is also a very important programming skill!

Guess you like

Origin blog.csdn.net/TTTSEP9TH2244/article/details/115733669
Recommended