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

标签 c return printf return-value scanf

#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 items successfully matched and assigned; this can be fewer than provided for, 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 the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set to indicate the error.

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

相关文章:

javascript - 重构 C 函数以在 O(n) 到 JavaScript 中查找二叉树的直径

java - 在 Catch 语句中返回

c - 尝试格式化我的 printf 以排列美元符号和美分 - c 源代码

c - 如何打印字母无效

c - 为什么相同的函数在链接其他目标文件后表现不同?

c - 带双引号的宏

javascript - 使用 Ext.Ajax.request 使 ExtJS 单例返回对象

c# - finally block 中抛出异常后的返回值会怎样?

c - 两个格式说明符但只有一个参数

python - 为 python hmmlearn 包编译 C 代码时出错