VC ++ 6.0 to debug using C language code
Step One: Compile
C language is a compiled language, so we added a breakpoint during debugging, unlike Python that can be added directly to the breakpoint in VC ++ 6.0, the just finished code is not directly increase breakpoint.
As shown above, this time has not been compiled, you can see the right side of the toolbar is not clicking a few buttons, only the leftmost compiled two buttons can be used. We must first of this code is compiled.
After compilation, we have produced a good exe file, then find the right number of buttons is ready for use.
Side note several functions of the buttons:
left to right: compilation, production, stop generating, generate and execute, debug, add / remove breakpoints
Step Two: Start Debugging
, You can start debugging the code by the previous step. The following is the code used in the demo:
#include<stdio.h>
int main()
{
int num1 = 5,num2 = 10;
char ch = 'A';
int i;
printf("num1=%d, num2=%d, ch=%c\n",num1,num2,ch);
for(i = 0;i < 4; i++)
{
num1 ++;
num2 += 2;
}
ch += 5;
printf("num1=%d, num2=%d, ch=%c\n",num1,num2,ch);
num1 /= 3;
num2 %= 5;
ch += 2;
printf("num1=%d, num2=%d, ch=%c\n",num1,num2,ch);
return 0;
}
这里将断点加在了这两个位置
然后点击调试按钮,或按快捷键F5。
第一次进行调试时,会弹出一个调试工具栏
关于里面各个按键功能不一一介绍,可自行尝试。如果关掉了这个窗口,可以通过鼠标右键工具栏,点击调试再找出来。
好了,接下来一步步开始调试。按下快捷键F10,可以一句一句向下走。
这里我们从第一个断点走到了第一个printf,下面会显示出已经用到的变量和变量值。
在执行到这一句时,两个整型变量num1,num2已经进行过初始化,而i未进行初始化,同时是一个局部变量,所以显示的值是随机分配给的一个值。字符变量ch的值中,A是它所代表的字符,65是这个字符的ASC码值。printf在stdio.h这个头文件里被定义成的是有返回值的,同样scanf也是,所以在调试时我们会看到它的返回值。(在一些开发环境中,如VS,使用printf或scanf都会报waring,提示忽略了它们的返回值,也是这个原因)
那么我们继续向下走。
这里是进入到了一个for循环,在按F9逐句调试时,会重复走{}括起来的这几行。如果循环次数过多,想直接结束对这个for的调试,可以按下shift+F11,也是直接跳出对函数调试的快捷键。
最后调试结束,按下F5,就可以终止调试,回到开发状态了。
可能会出现的问题
在调试执行下一步中,按下F9或点击调试工具栏里的按钮,有的电脑会出现这个提示。
原因是VC++和Win7及以后版本的兼容性有问题,毕竟其不是同一时代的产品。提示需要管理员的权限,其解决办法如下:首先选择VC++的快捷方式,右键单击,选择“以管理员身份运行”,再在菜单里选择“文件”打开你要调试的程序即可进行调试。