gpt4 book ai didi

C 输入与 scanf

转载 作者:行者123 更新时间:2023-11-30 17:44:41 25 4
gpt4 key购买 nike

在 C 编程语言中,scanf() 函数从键盘字符或数字读取?

例如,如果格式为 %d 并且我写 "1""a",则 scanf( ) 只读取整数并忽略其他字符?

我在一本书上看到,scanf()从键盘读取字符,然后将字符转换为格式指定的数据类型。

谁能给我解释一下。提前致谢。

最佳答案

如果你看function signature scanf() 的,它返回一个 int,您可以用它来检查扫描操作是否成功或失败:

On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.

If an encoding error happens interpreting wide characters, the function sets errno to EILSEQ.

关于C 输入与 scanf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19888676/

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