谁能解释以下程序扫描值的输出..?

标签 c printf output scanf

#include<stdio.h>

int main()
{
    int i;
    printf("%d\n", scanf("%d", &i) );
    return 0;
}

如果向 scanf() 提供一些值,程序的输出仍然是 1。为什么?

最佳答案

if some value is supplied scanf but the output of the program is still 1. Why?

因为 scanf 返回一个 int 作为 per the documentation . scanf 将返回扫描并分配成功的项目总数。

在您的例子中,如果 scanfstdin 中成功扫描了一个 int,它将返回 1,否则它将返回 0。它还将在遇到 EOF 时返回 -1。然后,此值由您拥有的 printf 打印。


如果要打印i的值,将scanfprintf分开,即使用

int i;
scanf("%d", &i)
printf("%d\n", i);

代替

int i;
printf("%d\n" , scanf("%d", &i) );

关于谁能解释以下程序扫描值的输出..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31154894/

相关文章:

c - 如何找到最高有效位位置?

c++ - 想要在Hudson C/C++上工作

c - 0 精度的 printf 0

c - 在数组中存储结构体时遇到问题

c - 预处理标记和空格

c - 在 C 中的一个 printf 中打印(部分)数组

c - 如何使 printf() 从宏中正确打印二进制 blob?

buffer - 如何增加 ipython qtconsole 回滚缓冲区限制

c - 可以从 C 语言的标准输出读取吗?

python - 使用换句话说,格式化字符串的长度