C语言 错误 C4996:“scanf”:此函数或变量在 c 编程中可能不安全
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30577519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error C4996: 'scanf': This function or variable may be unsafe in c programming
提问by Chheang Phearum
I have created a small application to find max number by using user-defined function with parameter. When I run it, it shows this message
我创建了一个小应用程序,通过使用带参数的用户定义函数来查找最大数量。当我运行它时,它显示此消息
Error 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.
错误 1 错误 C4996:'scanf':此函数或变量可能不安全。考虑使用 scanf_s 代替。要禁用弃用,请使用 _CRT_SECURE_NO_WARNINGS。详细信息请参见在线帮助。
What do I do to resolve this?
我该怎么做才能解决这个问题?
This is my code
这是我的代码
#include<stdio.h>
void findtwonumber(void);
void findthreenumber(void);
int main() {
int n;
printf("Fine Maximum of two number\n");
printf("Fine Maximum of three number\n");
printf("Choose one:");
scanf("%d", &n);
if (n == 1)
{
findtwonumber();
}
else if (n == 2)
{
findthreenumber();
}
return 0;
}
void findtwonumber(void)
{
int a, b, max;
printf("Enter a:");
scanf("%d", &a);
printf("Enter b:");
scanf("%d", &b);
if (a>b)
max = a;
else
max = b;
printf("The max is=%d", max);
}
void findthreenumber(void)
{
int a, b, c, max;
printf("Enter a:");
scanf("%d", &a);
printf("Enter b:");
scanf("%d", &b);
printf("Enter c:");
scanf("%d", &c);
if (a>b)
max = a;
else if (b>c)
max = b;
else if (c>a)
max = c;
printf("The max is=%d", max);
}
采纳答案by user3742467
It sounds like it's just a compiler warning.
听起来这只是一个编译器警告。
Usage of scanf_sprevents possible buffer overflow.
See: http://code.wikia.com/wiki/Scanf_s
的使用scanf_s可以防止可能的缓冲区溢出。
请参阅:http: //code.wikia.com/wiki/Scanf_s
Good explanation as to why scanfcan be dangerous: Disadvantages of scanf
关于为什么scanf会很危险的很好的解释:scanf 的缺点
So as suggested, you can try replacing scanfwith scanf_sor disable the compiler warning.
因此,作为建议,可以尝试更换scanf用scanf_s或禁用编译器警告。
回答by JerryGoyal
Another way to suppress the error: Add this line at the top in C/C++ file:
另一种抑制错误的方法:在 C/C++ 文件的顶部添加这一行:
#define _CRT_SECURE_NO_WARNINGS


