我的全栈之路-C语言基础之Visual Studio 2019使用技巧

我的全栈之路-C语言基础之Visual Studio 2019使用技巧

5.1 Visual Studio 2019调试程序

在后期编写程序时,经验不足的开发人员通常会遇到两种错误:编译错误和运行时错误,编译错误通常是编写的程序不满足编译器的语法规范,而运行时错误则是程序运行时发生的错误,想要排查程序运行时的错误,就需要使用IDE提供的调试功能。

在源文件debug.c中定义一个返回两个整数相乘的方法mul,然后在main函数中定义两个变量,并调用两个整数相乘的方法,输出计算结果

#include <stdio.h>
#include <stdlib.h>


/*
    定义两个整数相乘并返回
*/
int mul(int source,int target) {

    return source * target;
}
/*
    调试程序
    @author liuguanglei [email protected]
    @version 2019/08/21
*/
int main(int argc, char* argv[]) {

    //定义两个变量
    int source = 12;
    int target = 12;
    printf("source = %d \t target = %d \n", source,target);

    //调用两个整数相乘的方法,并将计算的结果保存到整数变量result中
    int result = mul(source,target);
    //输出两个整数相乘的结果
    printf("result = %d \n",result);
    system("pause");
    return 0;
}

在调试程序前首先需要下断点,可以使用快捷键F9下断点或者取消断点
调试

然后使用F5调试运行程序,程序会在之前下过断点的地方暂停,此时可以在Visual Studio提供的窗口中观察变量的值
调试

如果想要一行一行调试代码,可以使用快捷键F11,当遇到函数时(例如这里的mul函数),会进入函数的内部一行一行执行,直到该函数结束为止。

如果调试时不想进入函数的内部,可以使用快捷键F10,当程序中调用了其他函数时不会进入其他函数内部,但是函数会被调用。

如果调试时想要从一个断点跳到另外一个断点,可以使用shift+F11实现。

如果想要查看函数调用的定义,可以使用快捷键F12,退出查看函数定义则使用Ctrl+ -。
在调试代码的过程中,还可能会注释部分错误的代码,注释可以使用快捷键Ctrl+k,u实现,而取消注释可以使用Ctrl+k,u实现。

如果源文件中的代码格式比较凌乱,可以使用Ctrl+k,f快速格式化代码。

5.2 Visual Studio 2019解决4096错误

Visual Studio 希望用户使用Visual Studio提供的非标准库函数,但是程序员希望使用标准库函数,因为标准库是可以移植的。
如果在Visual Studio中使用标准的库函数,就会引发4996错误类似于这种提示:error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.这里的strcpy是标准的库函数,而在Visual Studio中使用这个函数引发了4996错误。

#define _CRT_SECURE_NO_WARNINGS //防止4996错误
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
    vs4996错误
     error C4996:  'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

    @author liuguanglei [email protected]
    @version 2019/08/21
*/
int main(int argc,char *argv[]) {
    //定义一个字符串
    char buf[32] = "";
    //使用strcpy实现将字符串hi string复制到变量buf中
    strcpy(buf, "hi string");
    //输出字符串的内容
    printf("buf = %s \n",buf);
    system("pause");
    return 0;

}

如何解决Visual Studio中的4996错误呢?

方案1:在源文件(例如这里的vs4996.c)的第一行定义宏

#define _CRT_SECURE_NO_WARNINGS //防止4996错误

方案2:在源文件(例如这里的vs4996.c)的第一行定义如下内容:

#pragma warning(disable:4996)

5.3 Visual Studio 2019配置代码片段

在后期编写C程序中,需要反复使用到如下的代码片段

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <stdlib.h>
/*

    @author liuguanglei [email protected]
    @version
*/
int main(int argc,char *argv[]) {

    
}

这里为了减轻工作量,使用Visual Studio 2019提供的代码片段管理器来实现通过输入配置的快捷键生成模板代码。

首先自定义代码片段文件MyCCode.snippet

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>MyCCodeTemplate</Title>
            <Shortcut>mcct</Shortcut>
            <Description>c的标准模板代码</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
                <SnippetType>SurroundsWith</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Code Language="cpp">
            <![CDATA[#define _CRT_SECURE_NO_WARNINGS
            #include <stdio.h>
            #include <stdlib.h>
      /*
        
          @author liuguanglei [email protected]
          @version 
      */
            int main(int argc, char *argv[])
            {
                system("pause");
                return 0;
            }]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

然后进入Visual Studio 2019 菜单 工具->代码片段管理器
自定义代码片段

选择代码片段管理器的语言为C++,然后选中My Code Snippets
自定义代码片段

把MyCCodeTemplate.snippet导入进来
自定义代码片段

自定义代码片段
自定义代码片段

自定义代码片段
自定义代码片段

导入成功之后新建任意的C程序源文件,在源文件中输入快捷键mcct就可以生成如下的代码片段

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
/*

    @author liuguanglei [email protected]
    @version
*/
int main(int argc, char* argv[])
{
    system("pause");
    return 0;
}

想要了解代码片段的更多内容,请移步官网

猜你喜欢

转载自www.cnblogs.com/ittimeline/p/11404258.html