错误C4996:’scanf’:此函数或变量在c编程中可能不安全

我创建了一个小应用程序,通过使用带参数的用户定义函数来查找最大数量。 当我运行它时,它会显示此消息

错误1错误C4996:’scanf’:此函数或变量可能不安全。 请考虑使用scanf_s。 要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS。 详细信息请参见在线帮助。

我该怎么做才能解决这个问题?

这是我的代码

#include 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); } 

听起来这只是一个编译器警告。

scanf_s可防止可能的缓冲区溢出。
请参阅: http : //code.wikia.com/wiki/Scanf_s

关于为什么scanf可能是危险的很好的解释: scanf的缺点

因此,如建议的那样,您可以尝试使用scanf_s替换scanf或禁用编译器警告。

抑制错误的另一种方法:在C / C ++文件的顶部添加此行:

 #define _CRT_SECURE_NO_WARNINGS