C中的浮点异常(核心转储)

Floating point exception (core dumped) in C

在 fedora 32 中编写和编译 C 程序时,当我尝试从终端扫描用户输入时,我总是收到此消息 Floating point exception (core dumped),但我不知道我做错了什么。

我在中间放了一些 printf 函数来了解它在哪里停止,我发现 scanf ("%i", &number); 函数没有读取输入或正确保存它。

这是部分代码:

   #include <stdio.h>

int main (void)
{
    int number, constant, remain, remain1, mod, count, test;
    
    number = 0;
    mod = 1;
    count = 0;

    printf ("Enter a number to write: \n");
    scanf ("%i", &number);
    
    printf ("-1");
    
    constant = number;
    test = number;
    printf ("0");
    if (number < 0)
    {
        number = number * -1;
        printf ("-");
    }
    printf ("1");
    while (test != 0)
    {
        test /= 10;
        count++;
        printf ("%i", count);
    }
    printf ("2");
    for (int i = 1; i < count; i++)
    {
        mod = mod * 10; 
    }
    printf ("3");
    while (number != 0)
    {
        
        remain = number % mod;
        remain1 = constant - remain * mod;
        
        switch (remain)
        {
            case 0:
                printf ("Zero");
                break;
            case 1:
                printf ("One");
                break;
            case 2:
                printf ("Two");
                break;
            case 3:
                printf ("Three");
                break;
            case 4:
                printf ("Four");
                break;
            case 5:
                printf ("Five");
                break;
            case 6:
                printf ("Six");
                break;
            case 7:
                printf ("Seven");
                break;
            case 8:
                printf ("Eight");
                break;
            case 9:
                printf ("Nine");
                break;
        }

        remain = remain1;
        mod /= 10;
    }
    
        printf ("\n");
    
    return 0;
}

编辑我最初的猜测 - 它被零除

        if(!mod ) // add this if 
             {printf("Division by zero");exit(0);} //
        remain = number % mod;

始终检查 scanf 的结果。 Yuo 在这里没有任何浮点运算,但可能在您未显示的代码中某处被零除。

#include <stdio.h>

int main (void)
{
    int number, constant, remain, remain1, mod, count, test;
    
    number = 0;
    mod = 1;
    count = 0;

    printf ("Enter a number to write: \n");
    if(scanf ("%i", &number) != 1)
    {
        printf("scanf error\n");
    }
    else
    {    
        printf ("-1");
        
        constant = number;
        test = number;
        printf ("0");

        if (number < 0)
        {
            number = number * -1;
            printf ("-");
        }

        printf ("1");

        while (test != 0)
        {
            test /= 10;
            count++;
            printf ("%i", count);
        }

        printf ("2");

        for (int i = 1; i < count; i++)
        {
            mod = mod * 10; 
        }
    }
}

这里简化了这个函数:

char *digits[] = {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};

void printWord(int x)
{
    int work = x;
    long val = 1;
    while(work)
    {
        val *= 10;
        work /= 10;
    }
    val /= 10;
    if(x < 0) printf("Minus");
    if(!x) val = 10;
    do
    {
        printf("%s", digits[abs(x / val)]);
        x %= val;
        val /= 10;
    }while(x);
}

int main (void)
{
    printWord(0);    
    return 0;
}

https://godbolt.org/z/q4oWqT

这条语句:

mod /= 10;

最终导致 mod 包含 0。然后这个语句:

remain = number % mod;

除以 0 运算结果