C-检查输入(浮点)是纯整数还是浮点数

我想检查用户输入是纯整数还是浮点数。 我试图通过使用floorceilf并将值与函数中的原始x值进行比较来尝试这样做。 然而,这似乎有点问题,因为当floor(5.5)!=5.5ceilf(5.5)!=5.5时,函数对于某些数字(如5.5)保持返回0而不是1。 这是我的代码:

 #include  #include  #include  #include  #include  int intchecker(float x)//in a separate file { if (floor(x)==x && ceilf(x)==x) { //printf("%f",floor(x)); return 0; } else { return 1; } } int main() { char line[] = " +----+----+----+----+----+----+----+----+----+----+---+"; char numbers[] = " 0 5 10 15 20 25 30 35 40 45 50"; float balls,slots; int slot[9]; printf("==========================================================\nGalton Box Simulation Machine\n==========================================================\n"); printf("Enter the number of balls [5-100]: "); scanf("%f",& balls); if (balls>100 || balls<5){ printf("/nInput is not within the range. Please try again."); } else if (intchecker(balls)==1){ printf("/nInput is not an integer. Please try again."); } else { printf(" This is an integer."); //some more code here } } 

我尝试将intchecker代码放在另一个项目中,这似乎工作正常没有任何错误,不像以前的项目,当我使用printf语句检查floor(x)值是否正确时,它保持显示不同的答案,例如输入为5.2时为“-2.000000”。 这是我的第二个项目的代码:

 #include  #include  #include int main() { float x; scanf("%f",&x); if (floor(x)==x && ceilf(x)==x){ printf("Integer"); return 0; } else { printf("Non-Integer"); return 1; } } 

当第一个代码没有时,第二个代码如何正常工作? 我的写作/调用函数的方式有问题吗?(我相对较新的函数 – 到目前为止只有2周的曝光)

我在网上搜索并看到很多答案来检查输入是否为整数或浮点数,即使在stackoverflow.com本身,但我希望不是找出其他方法来检查输入是整数还是浮点数(如果我希望这样做) ,我可以谷歌它,并在stackoverflow.com上也有很多这样的问题),但要理解为什么我的第一个代码不起作用,因为据我所知,它应该运行良好,没有任何错误它目前面临着。

任何帮助是极大的赞赏!:)

假设缺少函数声明:

main.c缺少int intchecker(float x)的原型,因此main.c假定int intchecker(int x)的旧学原型,代码表现出未定义的行为。 什么事情都可能发生。

main.c添加原型或将其放在separate.h中,并在此处和separate.c中包含该头文件

 #include  #include  #include  #include  #include  int intchecker(float x); int main(void) { ...