. scanf 函数是有返回值的,它的返回值可以分成三种情况
- 正整数,表示正确输入参数的个数。例如执行 scanf("%d %d", &a, &b);
如果用户输入"3 4",可以正确输入,返回2(正确输入了两个变量);
如果用户输入"3,4",可以正确输入a,无法输入b,返回1(正确输入了一个变量)。 - 0,表示用户的输入不匹配,无法正确输入任何值。如上例,用户如果输入",3 4",返回0。
- EOF,这是在stdio.h里面定义的常量(通常值为-1),表示输入流已经结束。在Windows下,用户按下CTRL+Z(会看到一个^Z字符)再按下回车(可能需要重复2次),就表示输入结束;Linux/Unix下使用CTRL+D表示输入结束。
所以可以使用下面的代码来处理输入:
while (scanf("%s %c %c", str, &oldchar, &newchar) == 3) /* 或!= EOF , 但前者更好 */
{
; //处理
}
-
为什么前面 scanf 的格式串里面,%s和%c中间需要空格呢?
那是因为如果没空格的话。。。oldchar输入的就是空格了= =. -
顺便说一下,printf的返回值是输出的字符数,例如,printf("1234")的返回值是4,而printf("1234\n")的返回值是5。
https://www.e-learn.cn/content/wangluowenzhang/88661
prog.c: In function ‘main’:
prog.c:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
The writer's of your libc have decided that the return value of scanf should not be ignored in most cases, so they have given it an attribute telling the compiler to give you a warning.
If the return value is truly not needed, then you are fine. However, it is usually best to check it to make sure you actually successfully read what you think you did.
In your case, the code could be written like this to avoid the warning (and some input errors):
#include
//
// Created by Dxoca on 2020/12/10.
//1011 A+B 和 C
//
#include<stdio.h>
int main() {
long a, b, c;
int n;
if (scanf("%d", &n) == 1) {
for (int i = 0 + 1; i < n + 1; i++) {
if (scanf("%ld%ld%ld", &a, &b, &c) == 3) {
if (a > c - b) {
printf("Case #%d: true\n", i);
} else {
printf("Case #%d: false\n", i);
}
}
}
}
return 0;
}
本文地址:https://dxoca.cn/StudyNotes/384.html 百度已收录
版权说明:若无注明,本文皆为“Dxoca's blog (寒光博客)”原创,转载请保留文章出处。
快递代发,礼品代发快递单号上88单号网www.88danhw.com
赞一个