warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-resu

Posted liuyangfirst

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-resu相关的知识,希望对你有一定的参考价值。

这个是C语言当中常见的错误,意思是

     对于输入的scanf参数的内容,没有进行类型判断,所以才会产生这个问题.

解决方法:

     1、添加if判断方式

技术图片
1 if(scanf("%d",&a)==1){
2     // 成功继续执行其他代码
3  }
View Code

    2、其它类型判断方式扩展

技术图片
 1 if(scanf("%d",&a)==1){
 2     // 成功继续执行其他代码
 3 }
 4 
 5 if(scanf("%f",&a)==1){
 6     // 成功继续执行其他代码
 7 }
 8 
 9 if(scanf("%s",&a)==1){
10     // 成功继续执行其他代码
11 }
View Code

    3、参数个数扩展

技术图片
1 scanf("%d",&a);正确输入则返回1;
2 scanf("%d%d",&a,&b);正确输入,则返回2,以此类推;
View Code

即:如果输入一个参数,就判断结果是否==1,如果输入两个参数就判断是否==2,如果输入三个参数就判断==3,以此类推

技术图片
1 if(scanf(“%d %d",&a,&b)==2){
2    // 功能代码
3 }
4 
5 if(scanf(“%d %d %d",&a,&b,&c)==3){
6    // 功能代码
7 }
View Code

 

以上是关于warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-resu的主要内容,如果未能解决你的问题,请参考以下文章

支持ignore的`v-clickoutside`指令

是否需要 break 语句或 return 语句是否足够?

Linux修改ipv6为ignore

用英文解释ignore.再造个例句

线程相关工具类

关于形参与实参