为什么在整数变量中输入浮点值给我一个奇怪的答案?

时间:2015-09-12 17:09:09

标签: c

在以下C程序中

#include<stdio.h>
int main()
{
    int a,b;
    printf("Enter the values of a and b : ");
    scanf("%d %d",&a,&b);
    printf("%d %d",a,b);
    return 0;

}

如果我给出值&#34; 3.2,&#34;我得到的答案是= 3 1 有人可以解释一下发生了什么吗?我正在使用DEV C ++编译器。

1 个答案:

答案 0 :(得分:2)

函数scanf正在尝试读取两个int值。您为3.2提供了3,并在a中显示.,并停留在.2

然后尝试将b读入.但却失败,因为whitespace不被视为b

因此永远不会设置1,函数返回int b=42以显示转换为1的输入。

您可以通过初始化scanf来展示这一点,然后在b之后发现42仍然是SELECT * FROM x WHERE a = 1 AND b = 2 AND c = 3 UNION ALL SELECT * FROM x WHERE a = 1 AND b = 2 AND c = 0 AND NOT EXISTS (SELECT * FROM x WHERE a = 1 AND b = 2 AND c = 3) UNION ALL SELECT * FROM x WHERE a = 1 AND b = 0 AND c = 0 AND NOT EXISTS (SELECT * FROM x WHERE a = 1 AND b = 2 AND c = 3) AND NOT EXISTS (SELECT * FROM x WHERE a = 1 AND b = 2 AND c = 0)