原文地址: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. GIMP永久保存选择的办法

    选择选区,然后把选区放到channel里面去 这是一张已经选择好的选区的图片 然后选择select下的Save to Channel, 需要这部分选区的话,只需要点击这个按钮就可以了

  2. memory bist lib

    model NVIC_REG6T_1024x32(resetb,margin,clk,en,we,addr,d,q) ( bist_definition( clock clk high; chip_e ...

  3. linux-命令学习-1

    1. cat命令 http://blog.csdn.net/jackalfly/article/details/7556848 cat主要有三大功能:1.一次显示整个文件.$ cat   filena ...

  4. 【C#】【数据结构】002-线性表:单链表

    C#数据结构:单链表 1.自定义单链表结构: 单链节点类 using System.Collections; using System.Collections.Generic; using Unity ...

  5. Luogu 2569 [SCOI2010]股票交易 (朴素动规转移 + 单调队列优化)

    题意: 已知未来 N 天的股票走势,第 i 天最多买进 as [ i ] 股每股 ap [ i ] 元,最多卖出 bs [ i ] 股每股 bp [ i ] 元,且每天最多拥有 Mp 股,且每两次交易 ...

  6. poj2217 Secretary 后缀数组

    #include <iostream> #include <cstring> #include <string> #include <cstdio> u ...

  7. jsp jstl标签库 el表达式

    一.JSTL标签是什么? 提供了对国际化(I18N)的支持,它可以根据发出请求的客户端地域的不同来显示不同的语言. 同时还提供了格式化数据和日期的方法.实现这些功能需要I18N格式标签库(I18N-c ...

  8. centos dhcp 服务器搭建 多vlan

    centos dhcp 服务器搭建   多vlan centos 6.5   版本     /etc/dhcp/dhcpd.conf         服务器配置文件 /etc/rc.d/init.d/ ...

  9. Relay Race (DP)

    Furik and Rubik take part in a relay race. The race will be set up on a large square with the side o ...

  10. Result实现类

    package org.apache.struts2.dispatcher; import com.opensymphony.xwork2.ActionInvocation; import com.o ...