在VS中编译 C 语言项目,如果使用了 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.

原因是Visual C++ 使用了更加安全的 run-time library routines 。新的Security CRT functions(就是那些带有“_s”后缀的函数),请参见

《CRT函数的安全增强的版本》

下面给出这个问题的解决方案:

方法一:将原来的旧函数替换成新的 Security CRT functions。

方法二:用以下方法屏蔽这个警告:

  1. 在预编译头文件stdafx.h里(注意:一定要在没有include任何头文件之前)定义下面的宏:
#define _CRT_SECURE_NO_DEPRECATE
  1. 或声明
#pragma warning(disable:4996)
  1. 更改预处理定义:

    项目->属性->配置属性->C/C++ -> 预处理器 -> 预处理器定义,增加:

_CRT_SECURE_NO_DEPRECATE

方法三:方法二没有使用更加安全的 CRT 函数,显然不是一个值得推荐的好方法,但我们又不想一个一个地改函数名,这里还有一个更简便的方法:

在预编译头文件 stdafx.h 里(同样要在没有include任何头文件之前)定义下面的宏:

#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1

在链接的时候便会自动将旧函数替换成 Security CRT functions 。

注意:这个方法虽然使用了新的函数,但是不能消除警告,你还得同时使用方法二(-_-)。即实际应在预编译头文件 stdafx.h 里加入下面两句:

#define _CRT_SECURE_NO_DEPRECATE

#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1

错误原因解释:

这种微软的警告,主要因为那些C库的函数,很多函数内部是不进行参数检测的(包括越界类的),微软担心使用这些会造成内存异常,所以就改写了同样功能的函数,改写了的函数进行了参数的检测,使用这些新的函数会更安全和便捷。关于这些改写的函数你不用专门去记忆,因为编译器对于每个函数在给出警告时,都会告诉你相应的安全函数,查看警告信息就可以获知,在使用时也再查看一下MSDN详细了解。

参考资料

更多参考

VS 编译错误【error C4996: 'scanf': This function or variable may be unsafe. 】的解决方案的更多相关文章

  1. 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 ...

  2. [转]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 中编译 ...

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

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

  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. Visual Studio 2012 编译错误【error C4996: 'scanf': This function or variable may be unsafe. 】的解决方案(转载)

    转载:http://www.th7.cn/Program/c/201303/127343.shtml 原因是Visual C++ 2012 使用了更加安全的 run-time library rout ...

  6. 错误 1 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. d:\users\vs2013\le

    #define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>void main(){    int nu ...

  7. 错误 1 error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead

    错误简介 在VS 2012 中编译 C 语言项目,如果使用了 scanf 函数,编译时便会提示如下错误: 原因是Visual C++ 2012 使用了更加安全的 run-time library ro ...

  8. error C4996: 'scanf': This function or variable may be unsafe.

    项目属性-配置属性-c/c++-预处理器- 在下面的编辑窗口中添加一句命令:_CRT_SECURE_NO_WARNINGS 添加完成后应用并退出 http://jingyan.baidu.com/al ...

  9. 配置OpenCV产生flann\logger.h(66): error C4996: 'fopen': This function or variable may be unsafe问题[zz]

    使用vs2012/2013配置opencv编译出现问题: 1>------ 已启动生成: 项目: Win32ForOpenCV245, 配置: Debug Win32 ------ 1> ...

随机推荐

  1. kubernetes命令详情

    查看客户端和服务器侧的版本信息 kubectl version 列出当前版本的kubernetes的服务器端所支持的api版本信息 kubectl api-versions 查看帮助,语法格式 kub ...

  2. mysql语句,插入id随机生成

    insert into 表名 VALUES(uuid(),…) 还有一个uuid_short(),只有数字 insert into 表名 VALUES(uuid_short(),…)

  3. xgboost 最优参数, df某一个字段进行字符串搜索

    0.909323  with:   {'max_depth': 6, 'min_child_weight': 0.8, 'n_estimators': 800} df_huoguo = df[df.c ...

  4. 中国省市区json数据 三级联动

    <label> <span>购买地址</span> <select name="PurchaseProvince" style=" ...

  5. Summary: Calculate average where sum exceed double limits

    What is a good solution for calculating an average where the sum of all values exceeds a double's li ...

  6. com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value '��� mysql-installer-community-8.0.15.0

    <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &l ...

  7. Dungeon Master (广搜)

    问题描述: You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed ...

  8. openGL实现图形学扫描线种子填充算法

    title: "openGL实现图形学扫描线种子填充算法" date: 2018-06-11T19:41:30+08:00 tags: ["图形学"] cate ...

  9. asp.net的<% %>特定用法

    在asp.net应用程序中,在asp.net页面常用的<%@ %>.<%# %>.<%= %>.在全球化的项目中使用<%$ %>绑定资源项目,在asp. ...

  10. 2018软件工程W班第一次助教小结

    我是数计学院实验教学中心的一名老师,机缘巧合之下,这个学期跟着汪老师上<软件工程实践>这门课.之前有陆续听说过<构建之法>这本书,记得好像学院还有主办过研讨会.对于这门实践课, ...