gpt4 book ai didi

c - 为什么此代码为所有输入打印 "1"?

转载 作者:太空宇宙 更新时间:2023-11-04 06:59:42 24 4
gpt4 key购买 nike

#include <stdio.h>

int main(){
char testChar;

printf("%d\n", scanf("%c", &testChar));

return 0;
}

我只想打印扫描的字符数。但是,当我所有的输入都具有相同的输出时,即 1。

最佳答案

scanf()返回成功扫描的项目数。所以,在你所有的运行中 scanf() 都成功了,因此你的程序打印了 1。

来自 scanf() :

On success, these functions return the number of input itemssuccessfully matched and assigned; this can be fewer than providedfor, or even zero, in the event of an early matching failure.

The value EOF is returned if the end of input is reached before either thefirst successful conversion or a matching failure occurs. EOF is alsoreturned if a read error occurs, in which case the error indicator forthe stream (see ferror(3)) is set, and errno is set to indicate theerror.

关于c - 为什么此代码为所有输入打印 "1"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40157015/

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