gpt4 book ai didi

c - 为什么scanf总是返回1?

转载 作者:行者123 更新时间:2023-11-30 21:42:05 27 4
gpt4 key购买 nike

#include <stdio.h>    

int main() {
int days = scanf("%d", &days);
printf("%d", days);
return 0;
}

无论如何,结果都是 1。

7
1

我已经使用过很多次 scanf 但从未遇到过这种情况。这是怎么回事?

最佳答案

这是正确的,因为 scanf() 返回成功匹配和转换的元素的数量。考虑到您的情况的正确输入,每次您的输入都通过转换,因此您会看到值 1。

需要注意的是,scanf() 不返回扫描的值本身,它将值存储在传递的参数中。

引用 C11,第 §7.21.6.4 章

[...] the scanf 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.

关于c - 为什么scanf总是返回1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43146375/

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