2 weixin 35993567 weixin_35993567 于 2016.09.19 10:20 提问

'main' : undeclared identifier怎么解决

#include
int fun(int n)
{
int a[10000],i,j,count=0;
for(i=2;i<n;i++)
a[i]=1;
i=2;
while (i<n)
{
for (j=a[i]*2;j<=n;j+=a[i])
a[j] = 0;
i++;
while (a[i]==0)
i++;
}
printf("\nThe prime number between 2 to %d\n",n);
for (i=2;i<=n;i++)
if (a[i]!=0)
{ count++;
printf(count%15?"%5d":"\n%5d",a[i]);
return count;
}
main ()
{
int n=20,r;
r=fun(n);
printf("\nThe number of prime is :%d\n",r);

    }

}

2个回答

shihengzhen101
shihengzhen101   Rxr 2016.09.19 10:36
已采纳

大哥,你把main函数都写到函数fun里边了,先看看基础,洗衣歌简单点的程序,修改后如下

 int fun(int n)
{
    int a[10000],i,j,count=0;
    for(i=2;i<n;i++)
        a[i]=1;

    i=2;
    while (i<n)
    {
        for (j=a[i]*2;j<=n;j+=a[i])
            a[j] = 0;

        i++;
        while (a[i]==0)
            i++;
    }
    printf("\nThe prime number between 2 to %d\n",n);
    for (i=2;i<=n;i++)
        if (a[i]!=0)
        { 
            count++;
            printf(count%15?"%5d":"\n%5d",a[i]);
            return count;
        }
}

int main ()
{
    int n=20,r;
    r=fun(n);
    printf("\nThe number of prime is :%d\n",r);

}

Marksinoberg
Marksinoberg   Ds   Rxr 2016.09.19 10:27

看你的头文件有点问题,应该是

#include <stdio.h>
#include<stdlib.h>
··· ···

吧。

Csdn user default icon
上传中...
上传图片
插入图片