C 运行时错误(程序应该打印前 1000 个素数)

标签 c runtime

我在其他程序中使用了 isPrime() 函数,它运行得很好,我什至之前也以同样的方式引用过它。由于某种原因,该程序中的功能无法正常工作。我使用 printf() 来检查函数返回的内容,它似乎是内存位置。但我不知道要更改什么,因为正如我所说,我确信该功能可以正常工作。

#include <stdio.h>

int main(void){

 int isPrime(int a);
 int result;
 int x = 1;
 while(x <= 1000){

     result = isPrime(x);
     if (result == 1){
        printf("%d\n",x);
    }
    x++;
 }
 }

 int isPrime(int a){

 int count;
 int z;
 if(a == 1){
   return 0;
 } else {

   for (z = a; z != 0; z-- ){
      if(a % z == 0){
         count++;
      }
   }
   if(count <= 2){
      return 1;
   } else {
     return 0;
   }
 }  
}

最佳答案

计数初始化为0。

除此之外,在我的系统上它运行良好。

关于C 运行时错误(程序应该打印前 1000 个素数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15920552/

相关文章:

c - 使用 stdio 写入文件

c - 如何加速我的代码?

python - 更新后 Google Colab 无法连接到本地运行时

.net - 运行时异常是如何抛出的

scala - 如何对泛型类型参数进行模式匹配?

c++ - fuse (用户空间中的文件系统)错误 : expected primary-expression before ‘.’ token

c - 使用 ftok() 重复键

visual-c++ - 如何正确安装 MS VC++ 9 运行时?

javascript - 在 Qualtrics 中使用运行时数据实时执行简单的数学运算

c - C 中的符号扩展、二进制加法和减法