发生错误时Scanf返回1而不是0

Joh*_*ohn 2 c return input scanf c99

我已经读过,返回的值scanf指示它是否能够读取和转换值。我已经使用返回值来检测输入是否为整数,但是输入是否4.5 scanf返回1而输入是否g scanf返回0。她是我的代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x;
    int ReturnedValue=scanf("%d",&x);
    printf("Scanf Returned:%d",ReturnedValue);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在多篇不同的文章中,您建议使用返回的值scanf检测整数,但是为什么现在不起作用?

the*_*bee 5

返回值不表示成功或失败。它是成功转换的值的数量。

这就是为什么您在“ 4.5”上获得“ 1”的"%d"格式,然后转换了“ 4”的原因。

通过阅读的文档,您会发现这一点scanf()