注册 登录
编程论坛 C语言论坛

一个报错的修改 warning: ignoring return value of ‘scanf’, declared with attribute warn_

温柔 发布于 2019-11-12 21:52, 5079 次点击
#include <stdio.h>

int main ()
{
    struct student
    {
        char name[10];
        char no[10];
        float score;
    };
    int n;
    int i;
    scanf("%d",&n);
    struct student stu[n];
    struct student max;
    struct student min;
    for (i=0;i<n;i++)
    {
    scanf("%s %s %f ",stu[i].name,stu[i].no,&stu[i].score);
    }
    max=stu[0];
    min=stu[0];
    for(i=0;i<n;i++)
    {
   
        if (max.score<stu[i].score)
            max = stu[i];
        if (min.score>stu[i].score)
            min=stu[i];
    }
    printf("%s %s\n",max.name,max.no);
    printf("%s %s\n",min.name,min.no);
    return 0;
}
a.c:13:2: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ^~~~~~~~~~~~~~
a.c:19:2: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s %s %f ",stu[i].name,stu[i].no,&stu[i].score);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 回复
#2
rjsp2019-11-13 09:30
如果是OJ等,scanf一定成功,不需要判断其返回值。
咦,我才回过神来,你想问什么?
#3
纯蓝之刃2019-11-13 20:03
你这个应该只是告警吧,scanf是由返回值的,你的编译器的 [-Wunused-result]检查规则检查了返回值项目,你没有使用scanf的返回值,所以告警了。scanf会返回输入的参数的个数。所以你可以这样写
if((flag=scanf("%s %s %f ",stu[i].name,stu[i].no,&stu[i].score))==3)
1