错误C4996:'scanf':该函数或变量在C编程中可能不安全 [英] error C4996: 'scanf': This function or variable may be unsafe in c programming
问题描述
我创建了一个小应用程序,通过使用带有参数的用户定义函数来查找最大数目.当我运行它时,它显示此消息
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
错误1错误C4996:'scanf':该函数或变量可能不安全. 考虑改用scanf_s.要禁用弃用,请使用 _CRT_SECURE_NO_WARNINGS.有关详细信息,请参见在线帮助.
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.
该如何解决?
这是我的代码
#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);
}
推荐答案
听起来像只是编译器警告.
It sounds like it's just a compiler warning.
使用scanf_s
可以防止缓冲区溢出.
请参阅: http://code.wikia.com/wiki/Scanf_s
Usage of scanf_s
prevents possible buffer overflow.
See: http://code.wikia.com/wiki/Scanf_s
关于为什么scanf
可能是危险的很好的解释: scanf的缺点
Good explanation as to why scanf
can be dangerous: Disadvantages of scanf
因此,按照建议,您可以尝试将scanf
替换为scanf_s
或禁用编译器警告.
So as suggested, you can try replacing scanf
with scanf_s
or disable the compiler warning.
这篇关于错误C4996:'scanf':该函数或变量在C编程中可能不安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!