VS2015 scanf 函数报错 error C4996: 'scanf'
来源:互联网 发布:网络诈骗安全选择题 编辑:程序博客网 时间:2024/05/22 08:09
错误提示: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.c1>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),就可以避免此问题!
下面是一些参考:
(1)http://bbs.csdn.net/topics/390508208
(2)stackoverflow
1 0
- VS2015 scanf 函数报错 error C4996: 'scanf'
- VS2015 scanf 函数报错 error C4996: 'scanf'
- scanf 函数报错 error C4996: 'scanf'
- error C4996: 'scanf': This function or variable may be unsafe.Visual Studio系列编译器使用scanf函数报错
- error C4996: 'scanf'
- VS2013使用scanf、gets及字符串函数编译报错error C4996: 'scanf': This function or variable may be unsafe. 原因及解决方案
- VS2013运行报错error C4996: 'scanf': This function or variable may be unsafe.
- VS2015使用scanf报错解决方案
- VS2015:error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead
- 关于VS2013中scanf()函数的C4996错…
- vs2012安全函数 error C4996: 'scanf':may be unsafe. use _CRT_SECURE_NO_WARNINGS
- VS2015报错C4996处理
- VS2015 编译器报错C4996
- 关于scanf报错
- VS2015报错C4996处理,error C4996: 'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_D
- VS2015 scanf() scanf_s
- scanf出现c4996错误提示。
- VS2015异常 C4996 'scanf 'This function or variable may be unsafe
- UVALive - 4618 Wormholes(负环)
- 1064. Complete Binary Search Tree (30)
- getClass(),class属性和getName()解析getGenericSuperclass
- 树懒_移动开学笔记_day21_自定义通知_存储思路
- [C++]学习札记2015-08-21
- VS2015 scanf 函数报错 error C4996: 'scanf'
- 树懒_移动开学笔记_day22_pull解析与SQLite
- 关于离散数学
- 构造函数为什么不能是虚函数
- 这是一个测试帖子
- 经典语录
- Android学习之自定义view(一)
- Codeforces 276E(树状数组)
- android SDK Manager下载和更新速度慢甚至无法下载解决方案