首页 > 为什么scanf 函数报错 在C++文件当中。平台VS2013

为什么scanf 函数报错 在C++文件当中。平台VS2013

include<stdio.h>

include<stdlib.h>

void main()
{
int tmp;
printf("请输入需要查找的数值\n");
scanf("%d",&tmp);
system("pause");
}


楼上写的都不是重点,重点在于C++11对于字符串严格的溢出检查解决:你至于要把scanf换为scanf_s就行


英语可以的话,你应该能看懂编译器的提示,从vs某个版本开始,原始的stdio中的绝大多数输入输出函数例如printf都被标记上了unsafe,以前是warning,有可能是你开了-Wall,也可能是现在直接强制error了,换成printf_s这样的安全版本就可以了,用法基本类似,详情可以查阅msdn


c:\code\二分查找\二分查找\source.cpp(11): 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)\microsoft visual studio 12.0\vc\include\stdio.h(283) : see declaration of 'scanf'


vs2013有强制的安全检查,scanf属于不安全函数,在c+11 标准中推荐使用scanf_s,不想使用的话就可以在头部最开始的地方#define _CRT_SECURE_NO_WARNINGS 跳过安全检查!


头文件<cstdio.h>

【热门文章】
【热门文章】