素数的C二进制程序不想运行

C binary program for prime numbers does not want to run

我在执行程序时遇到问题。 我使用 gcc 编译它:gcc -std=c89 -Wall -Wextra -lm main.c。 clang 也有同样的问题,我还没有用 tcc 尝试过,但我会在程序完成时尝试。 我希望它可以用 c89 编译器编译,这解释了我的一些选择。我在编译时没有错误,但在执行时有问题。 就像我在代码中的一些评论中写的那样,在正常执行时,我会因为除以 0(使用 k 变量)而出错,但我认为我在 "do...while" 循环中出错(因为我的其余功能已经过测试),并且在 by k 除法之前。 我花了几个小时来调试(尤其是使用 gdb(-g 选项添加到编译命令和 run -v 在 gdb 中)但我仍然被阻止。

/*librairies*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/*main function*/
int main(){
    int N, b, i, j, h, k;
    int *t; /*pointer which will become an array (see in another after comment)*/
    float l, m, a;
    k = 0;   /*Problem with k because we will see later an error*/
    printf("Your number : ");
    scanf("%d", &N);
    if (N>1){
        a = sqrt(N);
        b = a; /*we convert float into int and it rounds under, normal*/ 
        t = malloc(N * sizeof(int)); /*manual dynamic memory allocation for the array to respect C89*/
        if (t==NULL){ /*t is null when you have not enough ram for a too big value for N*/
            exit(0);
        }
        for (i=1;i<N;i++){ /*function to fill the array with 2,3..N*/
            j = i + 1;
            h = i - 1;
            t[h] = j;
        }

        do{
            if (t[k]==0){
                k = k + 1;
            } else{
                for (i=1;i<N;i++){          /*problem nearby, because the division by 0 error does not come know at the execution = 0*/
                    h = i - 1;   
                    if (t[h]!=0){
                        l = (float)t[h] / (float)k; /*It's going to be a problem because in the first loop, k = 0*/
                        m = l - (int)l;
                        if (m==0.0f){
                            t[h] = 0;
                        }
                    }
                }
            }
        } while (k<b);
        h = 0;
        for (i=1;i<N;i++){
            h = i - 1; 
            printf("%d\n", t[h]); /*printf to test before continuing the program*/
        }

    } else{
        printf("Your number is not valid\n");
    }
    return 0;
}

信息:这个程序只是一个个人项目,为我说明我上过的一门数学课程(关于素数,并且知道解决了问题和解决了k问题,它应该显示:

Your number: N /*N is a valid (positive integer) stdin input*/
0 /*and 0 until the k<b prime values are passed but I will make them to be displayed*/
a prime number
0 
the next prime number
0 
the next prime number
0 
the next prime number
0 
the next prime number
0 
.
.
the last prime number (which can be N if N is a prime one)

这个程序使用的定理是:

谢谢。

以下建议代码:

  1. 干净地编译
  2. 执行所需的功能
  3. 效率不高
  4. 显示正确的缩进
  5. 显示有意义的变量名
  6. 在退出前正确地将分配的内存传递给 `free_
  7. 正确检查 scanf()
  8. 是否成功执行

现在,建议的代码:

/*librairies*/
#include <stdio.h>
#include <stdlib.h>


/*main function*/
int main( void )
{
    size_t targetNum;
    size_t i;  // index
    size_t j;  // index



    size_t *primeArray = NULL; /*pointer which will become an array (see in another after comment)*/

    printf("Enter Your target number : ");
    if( scanf("%lu", &targetNum) != 1 )
    {
        fprintf( stderr, "scanf failed to read target number\n" );
        exit( EXIT_FAILURE );
    }

    if ( targetNum > 1 )
    {
        primeArray = malloc( targetNum * sizeof( size_t ) ); 

        if ( !primeArray )
        { 
            perror( "malloc failed" );
            exit( EXIT_FAILURE );
        }

        // initialize array of prime numbers
        for ( i = 0; i < targetNum; i++ )
        { 
            primeArray[i] = 1;
        }

        primeArray[0] = 0;
        primeArray[1] = 0;

        // find primes and zero multiples
        for( i = 0; i < targetNum; i++ )
        {
            // skip any multiples of primes (which have already been zero'd
            if( !primeArray[i] )
            {
                continue;
            }

            // zero multiples
            for( j = i+i; j < targetNum; j+=i )
            {
                primeArray[j] = 0;
            }
        }

        // print primes
        for ( i = 0; i < targetNum; i++ )
        {
            if( primeArray[i] )
            {
                printf( "%lu\n", i ); 
            }
        }

        free( primeArray );
    } 

    else
    {
        printf("Your number is not valid\n");
    }

    return 0;
}

给定以下输入:

4

输出是:

Enter Your target number : 4
2
3