c - main方法没有被执行

标签 c

这是一个使用埃拉托斯特尼筛法查找素数的程序。程序正在编译,但在执行时,它变得无响应。打印语句本身不被执行。我能知道我哪里出了问题吗?

#include<stdio.h>
int main()
{
    printf("Enter the range");
    int n,i;
    scanf("%d",&n);
    int j;
    int a[--n];
    for(i=0;i<n;i++)
            a[i]=i+2;
    for(i=0;i<n;i++)
            if(a[i])
            {
                    printf("%d",a[i]);
                    for(j=2;(i*j)<n;j++)
                            a[i*j]=0;
            }
    return 0;
}

谢谢

最佳答案

你的程序第一次循环时是无限循环的。

当 i = 0 时,这个循环永远不会结束:

for(j=2;(i*j)<n;j++)

关于c - main方法没有被执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18863919/

相关文章:

c - 无法在需要整数返回类型的函数中返回 NULL

C - 如何通过 scanf 多次使用字符串

c++ - #include <filename> 和 #inlude "filename"特定于 LLVM/Clang 之间有什么区别?

C - 指向字符串数组的指针不起作用

c - 错误: expression must be a pointer to a complete object type

c - 为什么服务器在客户端应用程序处于 STOPPED 状态后等待客户端?

c - sendto 中的参数无效

c - 如何在内核模块的相同偏移量中预留内存

c - 如何在 C 中将整数值赋给 Unsigned Char*

c - 让一个简单的 Makefile 工作