gpt4 book ai didi

c - 为什么 scanf 会因 float 而失败?

转载 作者:行者123 更新时间:2023-12-02 05:26:11 25 4
gpt4 key购买 nike

当我写这个的时候,编译它,然后运行:

int x;   
scanf ("%d", &x);
while (x!=4) {
scanf ("%d", &x);
}
然后在插入 char 时或 double数字小于 4 则进入无限循环。当插入大于 4 的 double 时,它​​会终止。
对此有什么解释吗?

最佳答案

来自 C language standard (n1256) :

7.19.6.2 The fscanf function
...
4 The fscanf function executes each directive of the format in turn. If a directive fails, as detailed below, the function returns. Failures are described as input failures (due to the occurrence of an encoding error or the unavailability of input characters), or matchingfailures (due to inappropriate input).
...
7 A directive that is a conversion specification defines a set of matching input sequences, as described below for each specifier. A conversion specification is executed in the following steps:

8 Input white-space characters (as specified by the isspace function) are skipped, unlessthe specification includes a [, c, or n specifier. 250)

9 An input item is read from the stream, unless the specification includes an n specifier. An input item is defined as the longest sequence of input characters which does not exceed any specified field width and which is, or is a prefix of, a matching input sequence. 251) The first character, if any, after the input item remains unread. If the length of the input item is zero, the execution of the directive fails; this condition is a matching failure unless end-of-file, an encoding error, or a read error prevented input from the stream, in which case it is an input failure.

10 Except in the case of a % specifier, the input item (or, in the case of a %n directive, the count of input characters) is converted to a type appropriate to the conversion specifier. If the input item is not a matching sequence, the execution of the directive fails: this condition is a matching failure. Unless assignment suppression was indicated by a *, the result of the conversion is placed in the object pointed to by the first argument following the format argument that has not already received a conversion result. If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.


在第 10 段中添加了重点。 %d转换说明符要求输入文本格式为十进制整数。如果不是,则转换失败,导致转换失败的字符将保留在输入流中。进一步调用 scanf()%d转换说明符将阻塞在同一个字符上。
scanf()返回成功分配的数量;您需要检查此结果以查看转换是否成功,如下所示:
int x = 0;
while (x != 4)
{
int result = scanf("%d", &x);
if (result != 1)
{
printf("Last call to scanf() failed; exiting\n");
break;
}
}

不幸的是,您仍然有错误的输入卡在输入流中。有许多策略可以解决这个问题。您可以使用 getchar 删除有问题的字符然后再试一次:
while (x != 4)
{
int tmp;
if (scanf("%d", &tmp) == 0)
getchar();
else
x = tmp;
}

或者你可以尝试读到下一个换行符,假设所有剩余的输入都被 b0rked:
while (x != 4)
{
int tmp;
if (scanf("%d", &tmp) == 0)
while (getchar() != '\n')
;
else
x = tmp;
}

或者您可以尝试将输入读取为文本并使用 strtol() 转换为整数(我的首选技术):
char input[SOME_SIZE];
int x = 0;
...
while (x != 4)
{
if (fgets(input, sizeof input, stdin))
{
char *check;
int tmp = (int) strtol(input, &check, 10);
if (!isspace(*check) && *check != 0)
{
printf("%s is not a valid integer: try again\n", input);
}
else
{
x = tmp;
}
}
else
{
printf("Read error on standard input\n");
break;
}
}

这是更多的工作,但它允许您在分配给 x 之前捕获错误的输入。 .

关于c - 为什么 scanf 会因 float 而失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4016073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com