如何在不使用 isdigit 的情况下检查数字是否不是 C 中的整数?

how to check if the number is not an integer in C without using isdigit?

我一直在编写斐波那契数列的代码(迭代)。它扫描一个数字 n,然后输出 F(n),直到我输入 -1。问题是首先,我需要检查数字是否不是整数。如果不是整数,应该输出"error".

#include<stdio.h>
#include<ctype.h>

int main(){
    float n=0;
    int x=0,ver=0,Fn=0,last1=0,last2=0;

    scanf("%f",&n);
    ver=n;
    while(n!=-1){
        if(n-ver!=0 || !isdigit(ver)){
            printf("Error\n");
        }
        else if(n==1 || n==2){
            printf("1\n");
        } else{
            last1=1;
            last2=1;
            for(x=3;x<=n;x++){
                Fn=last1+last2;
                last1=last2;
                last2=Fn;
            }
            printf("%d\n",Fn);
        }
        getchar();
        scanf("%f",&n);
        ver=n;
    }
return 0;
}

我已经尝试使用 isdigit 和 !isdigit,但我仍然得到错误的输出。当我输入 .11$、1.-、1.23、KDhf 等内容时,它应该会输出错误

要查找斐波那契数列,您不需要 nfloat 类型。您可以使用任何整数类型。

要查看号码是否扫描成功,请检查 scanf() 的 return 值。

int n = 0;
if (scanf("%df",&n) != 1) {
    printf("Error\n");
    exit(EXIT_FAILURE);
}

并删除这部分:

    if(n-ver!=0 || !isdigit(ver)){
        printf("Error\n");
    }

isdigit() 只能处理个位数。