VS2015 scanf 函数报错 error C4996: 'scanf'
错误提示: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'的更多相关文章
- 解决VS2017中使用scanf函数报错的问题
我们在VS2017中如果使用C语言的scanf输入函数,编译的时候编译器会报error C4996: 'scanf': This function or variable may be unsafe. ...
- VS中C语言scanf函数报错
在VS中创建C项目使用scanf方法时会如下报错 解决方案 方法1:文件中设置 在第一行设置代码 #define _CRT_SECURE_NO_WARNINGS 或者添加警告忽略 #pr ...
- 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函 ...
- 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 ...
- 解决VS2015中出现类似于error C4996: 'scanf': This function or variable may be unsafe的安全检查错误
用习惯了VS老版本的人当刚使用VS2013的时候可能总遇到类似于这样的错误: error C4996: 'scanf': This function or variable may be unsafe ...
- VS 编译错误【error C4996: 'scanf': This function or variable may be unsafe. 】的解决方案
在VS中编译 C 语言项目,如果使用了 scanf 函数,编译时便会提示如下错误: error C4996: 'scanf': This function or variable may be uns ...
- 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 ...
- [转]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 中编译 ...
- 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 ...
随机推荐
- java课程课后作业05之动手动脑
一.使用Files. walkFileTree()找出指定文件夹下所有大于指定大小(比如1M)的文件 此代码没有使用walkfiletree,两者的差别在于walkfiletree在遍历文件的时候有一 ...
- js中级小知识3
函数: 函数在调用的时候,会形成一个私有作用域,内部的变量不会被外面访问,这种保护机制叫闭包.这就意味着函数调用完毕,这个函数形成的栈内存会被销毁. 有些时候我们不希望他被销毁. 函数归属谁跟它在哪调 ...
- gitlab提交代码
cd existing_foldergit initgit remote add origin http://10.26.1.9/root/yunlian.gitgit add .git commit ...
- VS调试时JSON格式文件加载不了
在使用VS2012进行调试时发现加载数据为JSON格式的都加载不了,应该是MIME类型没有正确设置的问题. 直接通过浏览器地址栏访问时会收到提示,根据提示在IIS EXPRESS的安装目录下面执行一条 ...
- day21 二十一、垃圾回收机制、re正则
一.内存管理 1.垃圾回收机制:不能被程序访问到的数据称之为垃圾 2.引用计数:引用计数是用来记录值的内存地址被记录的次数 每一次对值地址的引用都可以使该值的引用计数 +1 每一次对值地址的释放都可以 ...
- 2018-2019-2 20165330《网络对抗技术》Exp5 MSF基础应用
目录 基础问题 相关知识 实验目的 实验内容 实验步骤 离实战还缺些什么技术或步骤? 实验总结与体会 实验目的 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路 返回目 ...
- Codeforces 1062 - A/B/C/D/E - (Undone)
链接:http://codeforces.com/contest/1062 A - Prank - [二分] 题意: 给出长度为 $n(1 \le n \le 100)$ 的数组 $a[1 \sim ...
- 20165317Java实验五 网络编程与安全
实验五 网络编程与安全 一.中缀转后缀 参考http://www.cnblogs.com/rocedu/p/6766748.html#SECDSA 结对实现中缀表达式转后缀表达式的功能 MyBC.ja ...
- linux下查看网卡速率
转自:http://blog.csdn.net/liugongfeng/article/details/50263733 我们都知道ifconfig -a 可以列出所有网卡,但是怎么判读是千兆网卡还是 ...
- java框架之SpringBoot(14)-任务
使用 maven 创建 SpringBoot 项目,引入 Web 场景启动器. 异步任务 1.编写异步服务类,注册到 IoC 容器: package zze.springboot.task.servi ...