首次练PAT,第一题就出了warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d",&n);的错误。
它所提示的错误主要是指我忽略了scanf()函数的int型返回值,(该返回值是指scanf()正确读取的整数个数),但是我真的用不着这个返回值啊(苦笑脸)!
搜了搜一些大神的讲解,主要有两个方法,
第一个,用强制类型转换,即在scanf()函数前加上“(void)”,以此强制忽略其返回值;再提交,结果正确;
第二个,不忽略,添加一个if语句判断一下就可以了:
if (scanf("%d", &n) != 1) {
//此处继续写后续语句;
···
} else {
printf("Failed to read integer.\n");
}