gpt4 book ai didi

c - 错误 C4996 : 'scanf' : This function or variable may be unsafe in c programming

转载 作者:可可西里 更新时间:2023-11-01 12:31:04 26 4
gpt4 key购买 nike

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

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

最佳答案

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

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

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

所以按照建议,您可以尝试将 scanf 替换为 scanf_s 或禁用编译器警告。

关于c - 错误 C4996 : 'scanf' : This function or variable may be unsafe in c programming,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30577519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com