原文地址:http://www.cnblogs.com/gb2013/archive/2013/03/05/SecurityEnhancementsInTheCRT.html

在VS 2012 中编译 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++ 2012 使用了更加安全的 run-time library routines 。新的Security CRT functions(就是那些带有“_s”后缀的函数),请参见:

CRT函数的安全增强的版本

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

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

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

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

#define _CRT_SECURE_NO_DEPRECATE

2. 或声明 #param warning(disable:4996)

3. 更改预处理定义:

项目->属性->配置属性->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详细了解。

参考资料:《安全模板重载

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

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

  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

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

  7. 错误 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 ...

  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. Linux菜鸟起飞之路【二】Linux基本常识

    一.Unix操作系统基本常识 1.什么是Unix? Unix是一个计算机操作系统,是一个用来协调.管理和控制计算机硬件与软件资源的控制程序. 2.Unix操作系统的特点? 多用户与多任务.多用户表示在 ...

  2. docker系列之镜像服务器

    docker 的镜像服务器 docker-registry 是 docker 项目的组成部分. 前面在谈 docker 的命令时, 它的 pull/push 命令就是和镜像服务器打交道. 并且, do ...

  3. fshc之请求仲裁机制的代码分析

    always@(posedge spi_clk or negedge spiclk_rst_n) begin if(~spiclk_rst_n) arbiter2cache_ack_r <='b ...

  4. Processed foods make us fatter easily

    From Business Insider Here's an experiment: sit alone in a hospital room for two weeks and eat nothi ...

  5. LeetCode(108) Convert Sorted Array to Binary Search Tree

    题目 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...

  6. LeetCode(91) Decode Ways

    题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A ...

  7. 树形 dp

    // ACM训练联盟周赛     C. Teemo's tree problem There is an apple tree in Teemo's yard. It contains n nodes ...

  8. Matplotlib基本图形之条形图

    Matplotlib基本图形之条形图 条形图特点: 以长方形的长度为变量的统计图表用来比较多个数据分类的数据大小通常用于较小的数据集分析例如不同季度的销量,不同国家的人口 示例代码: import o ...

  9. 高级java、C#、php、SQL、JavaScript......+n多编程语言学习分享

    /*入园两周年纪念.在搬砖之路一去不返*/ //搬砖什么都好,就是有点伤Ctrl键. <div style="display:none;"> </div>

  10. 给定一颗二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。

    /* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x ...