【发布时间】:2020-03-08 17:37:49
【问题描述】:
#include <stdio.h>
int main()
{
int number; // We make 1 int named number
scanf("%d",&number); // We give to number some number xd
while (number!=0)// We made a loop while if number isn't 0 do that
{
printf("text");// print text
scanf("%d",&number); // and get again a number.
// So everything works well beside inserting some char instead of int.
// So what is the problem wont scanf return 0 so we exit the program not
// just printing a text all day? That's my first question.
}
return 0;
}
第二个问题是如何让程序从键盘读取数字,直到我为 ex '.' 输入一些特殊符号。是的,我们用循环来做,对吗?但是,如果我输入除数字以外的所有内容,scanf("%d",&something) 它如何给我回0?
【问题讨论】:
-
ASCII Table and Description -- 如果您扫描为字符而不是 int,则哪个字符构成
number == 0? (提示:无)?始终,始终验证用于获取用户输入的任何函数的返回(尤其是scanf)。 -
它不返回 0 吗?公元前是假的?
-
在决定是否终止循环时,不要只看
number的值,还(或只)看scanf本身的返回值 . -
ASCII 字符
'0'具有十进制值48。十进制值为零的字符是 nul-character (您不能输入)。while (scanf("%d",&number) == 1) puts ("text");呢? -
奖励:如果您输入的不是数字,
stdin未读 中还剩下什么?scanf会导致什么类型的故障? (matching 还是 input?)如果您需要在循环退出后获取更多输入怎么办?与man 3 scanf一起度过 45 分钟,然后为自己节省十倍的时间……