错误提示:error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use_CRT_SECURE_NO_WARNINGS. See online help for details.

具体如下,这是一个计算输入字符串长度的程序:

#include "stdio.h"

int main() {
char s[30];
char* p;
scanf("%s", s);
p = s;
while (*p != '\0'){ p++; }
printf("%d\n", p - s);
while (1);
return 0;
编译结果:
1>------ Build started: Project: Learnc, Configuration: Debug Win32 ------
1> inputandoutput.c
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(355,5): error MSB6006: "CL.exe" exited with code 2.
1>d:\five\cppproject\learnc\learnc\inputandoutput.c(8): error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\windows kits\10\include\10.0.10150.0\ucrt\stdio.h(1270): note: see declaration of 'scanf'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

解释:VS认为c标准函数不安全,进行了一些处理。

要去除这个错误,有三个方法:

(1)根据提示

在文件顶部加入一行:#define _CRT_SECURE_NO_WARNINGS

#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
int main() {
char s[30];
char* p;
scanf("%s", s);
p = s;
while (*p != '\0'){ p++; }
printf("%d\n", p - s);
while(1);
return 0;
}
运行成功!
(2)根据提示:

在文件顶部加入一行:#pragma warning(disable:4996)

#pragma warning(disable:4996)
#include "stdio.h"

int main() {
char s[30];
char* p;
scanf("%s", s);
p = s;
while (*p != '\0'){ p++; }
printf("%d\n", p - s);
while(1);
return 0;
}
运行成功!

(3)真正原因在与vs中的SDL检查。于是可以:右键单击工程文件-->属性(最后一个)-------->  c/c++  ------>SDL checks ------------> no.

改前:

改后:

然后运行成功!

tips:在新建项目时可以把SDL检查勾掉(默认是yes),就可以避免此问题!

原文:https://blog.csdn.net/jh0703/article/details/47820875

VS2015 scanf 函数报错 error C4996: 'scanf'的更多相关文章

  1. 解决VS2017中使用scanf函数报错的问题

    我们在VS2017中如果使用C语言的scanf输入函数,编译的时候编译器会报error C4996: 'scanf': This function or variable may be unsafe. ...

  2. VS中C语言scanf函数报错

    在VS中创建C项目使用scanf方法时会如下报错 解决方案 方法1:文件中设置 在第一行设置代码    #define _CRT_SECURE_NO_WARNINGS 或者添加警告忽略     #pr ...

  3. VS2013编译报错error C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

    解决方法有两个: 1. 在预编译头文件stdafx.h里(在没有include任何头文件之前)定义下面的宏: #define _CRT_SECURE_NO_DEPRECATE 2. 将sprintf函 ...

  4. vs2013/2015中scanf函数类似于error C4996: 'scanf': This function or variable may be unsafe的安全检查错误

    在使用vs2015时,遇到了scnaf函数安全性的问题,程序不能正常运行,错误如下: error C4996: 'scanf': This function or variable may be un ...

  5. 解决VS2015中出现类似于error C4996: 'scanf': This function or variable may be unsafe的安全检查错误

    用习惯了VS老版本的人当刚使用VS2013的时候可能总遇到类似于这样的错误: error C4996: 'scanf': This function or variable may be unsafe ...

  6. VS 编译错误【error C4996: 'scanf': This function or variable may be unsafe. 】的解决方案

    在VS中编译 C 语言项目,如果使用了 scanf 函数,编译时便会提示如下错误: error C4996: 'scanf': This function or variable may be uns ...

  7. Visual Studio 2012 编译错误【error C4996: 'scanf': This function or variable may be unsafe. 】的解决方案

    在VS 2012 中编译 C 语言项目,如果使用了 scanf 函数,编译时便会提示如下错误: error C4996: 'scanf': This function or variable may ...

  8. [转]Visual Studio 2012 编译错误【error C4996: 'scanf': This function or variable may be unsafe. 】的解决方案

    原文地址:http://www.cnblogs.com/gb2013/archive/2013/03/05/SecurityEnhancementsInTheCRT.html 在VS 2012 中编译 ...

  9. mysql5.7设置简单密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

    注:本文来源于<  mysql5.7设置简单密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy r ...

随机推荐

  1. Tomb Raider

    Lara Croft, the fiercely independent daughter of a missing adventurer, must push herself beyond her ...

  2. poj1696

    没看题解,搜了一下都是什么叉积凸包,根本没有必要用吧.. 显然这个题我们找夹角就可以了,根据高中的公式 a·b=|a|*|b|*cos<a,b> 所以用点积找一个 cos<a,b&g ...

  3. Vs code常用插件

    Vs code常用插件 1.View In Browser 由于 VSCode 没有提供直接在浏览器中打开文件的内置界面,所以此插件在快捷菜单中添加了在默认浏览器查看文件选项,以及在客户端(Firef ...

  4. phpstorm 找到文件修改历史

    对着需要查看修改历史的文件右单击:

  5. 特殊字符url编码以后再解码后出现错误(&not , &cent, &curren, &pound)

    Url编码的原内容是 “&notify_url=xxxx”  经过url编码以后再解码回来  “&not”的部分就变成了“¬” 解决方案:把原文里面待url编码的&符号先替换成 ...

  6. Android基础总结+SQlite数据库【申明:来源于网络】

    Android基础总结+SQlite数据库[申明:来源于网络] 基础总结篇之一:Activity生命周期:http://blog.csdn.net/liuhe688/article/details/6 ...

  7. torch.utils.data.DataLoader使用方法

    数据加载器,结合了数据集和取样器,并且可以提供多个线程处理数据集.在训练模型时使用到此函数,用来把训练数据分成多个小组,此函数每次抛出一组数据.直至把所有的数据都抛出.就是做一个数据的初始化. 生成迭 ...

  8. webstorm 介绍

    最新版2017 破解 注册时,在打开的License Activation窗口中选择“License server”,在输入框输入下面的网址: http://idea.iteblog.com/key. ...

  9. 开源搜素引擎:Lucene、Solr、Elasticsearch、Sphinx优劣势比较

    https://blog.csdn.net/belalds/article/details/82667692 开源搜索引擎分类 1.Lucene系搜索引擎,java开发,包括: Lucene Solr ...

  10. [py]js前端求和与flask后端求和

    index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...