gpt4 book ai didi

c - scanf ("%d%d", &x, &x) 是否定义明确?

转载 作者:太空狗 更新时间:2023-10-29 16:34:01 27 4
gpt4 key购买 nike

下面的代码是否定义明确?

#include <stdio.h>

int ScanFirstOrSecond(const char *s, int *dest) {
return sscanf(s, "%d%d", dest, dest);
}

int main(void) {
int x = 4;
ScanFirstOrSecond("5", &x);
printf("%d\n", x); // prints 5

// Here is the tricky bit
ScanFirstOrSecond("6 7", &x);
printf("%d\n", x); // prints 7
return 0;
}

换句话说,... 参数对它们有隐含的限制吗?

我找到的最适用的 C 规范是

The fscanf function executes each directive of the format in turn. ... C11dr §7.21.6.2 4

最佳答案

简短的回答是:是的,它被定义:

scanf 将尝试将来自 stdin 的字节序列转换为以 10 为底的整数,带有可选的初始空格和可选的符号。如果成功,该数字将存储到 x 中。 scanf 将再次执行这些步骤。返回值可以是EOF012,对于后2,最后一个数转换后将存储到 x 中。

长答案更微妙:

C 标准似乎确实指定值按格式字符串的顺序存储。引用 C11 标准:

7.21.6.2 The fscanf function

...

4 The fscanf function executes each directive of the format in turn. When all directives have been executed, or if a directive fails (as detailed below), the function returns.

...

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:

...

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.

...

16 The fscanf function returns the value of the macro EOF if an input failure occurs before the first conversion (if any) has completed. Otherwise, the function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.

本规范中的任何其他地方都没有提到对输出对象的任何访问。

然而标准的措辞似乎表明,如果 2 个指针指向同一个对象,行为可能会出乎意料:转换的结果放在格式后的第一个参数指向的对象中argument that has not already received a conversion result. 这句话有点模棱两可:that has not already received a conversion result 指的是什么?对象还是参数?对象接收转换结果,而不是指针参数。在你扭曲的例子中,对象 x 已经收到一个转换结果,所以它不应该收到另一个......但是正如 supercat 所指出的那样,这种解释是明显的限制,因为它意味着所有转换值存储到第一个目标对象中。

因此它看起来完全指定且定义明确,但可以完善规范的措辞以消除潜在的歧义。

关于c - scanf ("%d%d", &x, &x) 是否定义明确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35712349/

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