Language foundation of my whole stack of the road -C Visual Studio 2019 tips

Language foundation of my whole stack of the road -C Visual Studio 2019 tips

5.1 Visual Studio 2019 debugger

Compile errors and run-time errors, compilation errors are usually written in the program does not meet the compiler syntax specification, and runtime error is running: When the late programming, inexperienced developers often encounter two errors error occurs, you want to troubleshoot errors when the program is running, we need to use debugging features provided by the IDE.

Back to the definition of a method of multiplying two integers mul, then the two variables defined in the main function, and the method of multiplying two integers debug.c call in the source file, the output of the calculation result

#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;
}

Before debugger you first need to break down, you can use the shortcut key F9 or cancel a breakpoint breakpoint
调试

Place and then use F5 to debug the program running, the program will break down over before the pause, this time can be observed in the variable window in Visual Studio to provide value
调试

If you want to debug your code line by line, you can use shortcut keys F11, when it comes to function (for example, where the mul function), will enter the internal function execution line by line, until the end of the function.

If you do not want to enter an internal function, you can use shortcut keys F10, when the program calls will not enter other internal functions when other functions, but the function is called. When debugging

Want to jump to another breakpoint from a breakpoint, you can use shift + F11 to achieve if debugging.

If you want to view the definition of a function call, you can use shortcut keys F12, exit Review function is defined using Ctrl + -.
In the process of debugging code, the error may also comment section of code, comments can use shortcut keys Ctrl + k, u realize, and uncommented can use Ctrl + k, u realize.

If the source file code format messy, can use Ctrl + k, f quick formatting code.

5.2 Visual Studio 2019 to solve the 4096 error

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;
}

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

Guess you like

Origin www.cnblogs.com/ittimeline/p/11404258.html