#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
将返回扫描并分配成功的项目总数。
在您的例子中,如果 scanf
从 stdin
中成功扫描了一个 int
,它将返回 1,否则它将返回 0。它还将在遇到 EOF
时返回 -1
。然后,此值由您拥有的 printf
打印。
如果要打印i
的值,将scanf
和printf
分开,即使用
int i;
scanf("%d", &i)
printf("%d\n", i);
代替
int i;
printf("%d\n" , scanf("%d", &i) );
关于谁能解释以下程序扫描值的输出..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31154894/